Help with Regex

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Help with Regex

Post by yogi108 » 19 Oct 2017 11:57

Hi,

I´m very new to regular expressions, but a little things are working yet.

I have the following problem:

Text used - just a testing sample
<?xml version='1.0' encoding='UTF-8'
<notificationIconType>BUILTIN</notificationIconType>
<notificationIcon>FLASH</notificationIcon>
<customNotificationIcon></customNotificationIcon>
<title></title>
<message></message><group></group>
<sound>false</sound>
<largeIconEnabled>
<largeIcon></largeIcon>
<group></group><group>_in Arbeit</group>
<isGroupSummary>false</isGroupSummary>
<all>false</all>
<group>_in Arbeit</group>
</data>
Regex used:
(?s)<group>(.+?)</group>
As far as I understand
(?s) search also over linefeed \n
find <group> in the text followed by at least 1 character greedy (.+?)
followed by </group>

So far so good.
the result is
matches(text, '(?s)<group>(.+?)</group>', groups)
--> false

findAll(text, '(?s)<group>(.+?)</group>')
--> [<group></group>
<sound>false</sound>
largeIconEnabled>
<largeIcon></largeIcon>
<group></group>, <group>_in Arbeit</group>, <group>_in Arbeit</group>]

findAll(text, '(?s)<group>(.+?)</group>', true)
--> [[<group></group>
<sound>false</sound>
largeIconEnabled>
<largeIcon></largeIcon>
<group></group>, </group>
<sound>false</sound>
largeIconEnabled>
<largeIcon></largeIcon>
<group>], [<group>_in Arbeit</group>, _in Arbeit], [<group>_in Arbeit</group>, _in Arbeit]]

replaceAll(text, '')
--> <?xml version='1.0' encoding='UTF-8'
<notificationIconType>BUILTIN</notificationIconType>
<notificationIcon>FLASH</notificationIcon>
<customNotificationIcon></customNotificationIcon>
<title></title>
<message></message>
<isGroupSummary>false</isGroupSummary>
<all>false</all>
</data>
if I change the regex to
(?s)<group>(.*?)</group>
it works better, but I get all <group></group> (empty!) also.

What am I doing wrong?

thanks,
regards,
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Help with Regex

Post by Desmanto » 19 Oct 2017 16:26

Which one you wanna match? And what is the final goal?

If you try with the question mark,

Code: Select all

(?s)<group>(.+)</group>
(?s)<group>(.*)</group>
These 2 will be greedy and match from the beginning until the end.
By using ?, it will turn them into non-greedy version of .* or .+ you put there.

When you use .+, it will match at least one character inside, so <group></group> won't be matched, since there is nothing between them. Instead it will capture the multiline
<group></group>
<sound>false</sound>
<largeIconEnabled>
<largeIcon></largeIcon>
<group>
</group>
The whole blue one is the .+

But when you use .*, it can match the blank, so <group></group> can be matched. Since you put ? in front of it (non greedy), it will stop at first match, and second match. Anything between won't capture at all.
<group></group>
<sound>false</sound>
<largeIconEnabled>
<largeIcon></largeIcon>
<group></group>
If your main purpose is to extract data from xml, you can just use XPath(). It is more reliable.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Re: Help with Regex

Post by yogi108 » 20 Oct 2017 12:09

@Desmanto

Ok, I see, maybe too much info without the info which is needed to perform the task :-)

If I use the action "Export flows/widgets" and use the resulting xml for uploading to the forum
everybody who imports these flows will get the flows in the groups where they have been
in my workspace. For a backup it works as desired.
For uploading I like to rename the
<group>My group</group>
to
<group>New name</group>
and save it again as xml.

Studying the xml export I saw empty <group></group> which are not needed.

So first I want to get the <group>My group</group> names for choosing,
afterwards renaming/replacing and saving.

Hope it is clear,
Thanks and
regards
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Help with Regex

Post by Desmanto » 20 Oct 2017 15:06

Actually I know you wanna do something to the flow.
But I am just curious how your example is different than the usual flow.xml. There is no <data> element at the beginning of the xml.

A proper exported xml flow from automagic should have the group parsed with 4 spaces, followed by <group>, group name and then closed with </group>.

Code: Select all

    <group>Display</group>
Since we have 4 spaces to mark that it is the correct element tag, we need to put that into our regex as well.
We can use Xpath also, but since we wanna replace it, we should use regex instead. Xpath only for extracting the data.

Your idea to edit the flow is a little similiar to Econdoc's idea to rename the flow. Except, you need to edit the xml directly (you can rename it at the same time if you want)
So you need to take a peek at the flow "Rename flow before sharing" : viewtopic.php?f=3&t=7037

1. Use the same Send/share Intent Received Trigger.

2. But you don't need to parse too much. You only need to extract the path name to the exported xml. So the script is different.

Code: Select all

//split the stream uri to header and path to file
uri = findAll(stream_uri, '(.*/root)(/.*\\.xml)', true);

//group 1 is header, group 2 is path to xml file
namepath = uri[0][2];
3. We have the namepath point to the exported xml. We need to load it up for editing, use Init Variable Text File on {namepath}, UTF-8.

4. Use script, replace function to replace every group name with the "New Name", and save it to new variable

Code: Select all

edited = replaceAll(file_text, "    <group>.*</group>", "    <group>New Name</group>")
As you can see, there is 4 spaces before <group>. The .* is any group name before you share it. New Name is the string of the name you want.

5. Save the edited xml back to file. Use Write to File, same file path {namepath}, with the text of new edited content {edited}. Uncheck append, as you want to replace it.

6. Reshare the flow using Start Activity. It is similiar to the flow above, but the extra.STREAM is using stream_uri (the same stream_uri, since it is the same path name).
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Re: Help with Regex

Post by yogi108 » 21 Oct 2017 07:09

Hi,

I will have a closer look at the intent flow if I get some time for it.
The testing text is originally from an exported flow, just cut many peaces.

Regex is really interesting and I will investigate in it,

Thanks as always,
Regards,
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga

Post Reply