Regex with capturing groups?

891 views
Skip to first unread message

Glauber Lima

unread,
Feb 25, 2013, 7:29:13 PM2/25/13
to tas...@googlegroups.com
Hello!

I've been developing a new profile to get my current location info by querying Google's geolookup service.

This piece of XML represents part of the data I'm interested:

Subject:
<address_component>
<long_name>Ribeirão Preto</long_name>
<short_name>Ribeirão Preto</short_name>
<type>locality</type>
<type>political</type>
</address_component>

An this is my working regex to extract the city name using a capturing group:
<short_name>(.+)</short_name>\s+<type>locality</type>

Match:
<short_name>Ribeirão Preto</short_name><type>locality</type>
Capturing Group 1 Match:
Ribeirão Preto

I don't know if  Tasker can perform this, I mean, is it possible to Tasker to work with capturing groups?

tlamik

unread,
Feb 26, 2013, 1:34:14 AM2/26/13
to tas...@googlegroups.com
Hi,

I think You can save Your data to VARIABLE and then try Search and Replace function.

T

Dne úterý, 26. února 2013 1:29:13 UTC+1 Glauber Lima napsal(a):

Pent

unread,
Feb 26, 2013, 1:38:00 AM2/26/13
to Tasker

> I don't know if  Tasker can perform this, I mean, is it possible to Tasker
> to work with capturing groups?

Yes.

Did you try it ?

Pent

Glauber Lima

unread,
Feb 26, 2013, 10:40:22 AM2/26/13
to tas...@googlegroups.com
Sounds interesting!

I'll give it a try and post my results later on :)

Ralf Stuckenschnieder

unread,
Apr 18, 2013, 8:50:37 PM4/18/13
to Tasker

@Glauber
has the function for capturing groups works with you?

@Pent
have you tested the function for capturing groups. Namely, it works not with me.

please excuse my bad English.

Pent

unread,
Apr 19, 2013, 2:01:29 AM4/19/13
to Tasker
> @Pent
> have you tested the function for capturing groups. Namely, it works not
> with me.

Use $1 etc.

Pent

Wietse van Buitenen

unread,
Apr 19, 2013, 2:45:38 AM4/19/13
to tas...@googlegroups.com
you should also be able to get JSON data back, you can the use a JavaScriptlet to parse this data and access it
I'm using this method since last week and it's much easier and futureproof than RegEx imho..
you will need Android 2.3+ for JavaScript though

Teasüti

unread,
Jul 21, 2013, 10:58:00 AM7/21/13
to tas...@googlegroups.com
Could you write and example?
I tried this too, the capturing groups. But in Search and Replace task it gives me the full hit, not just those between these: ().
Just studying Regex and don't understand the Java version yet.

Is there a way to inspect a string I don't know in advance, and need to investigate a particular detail that is between other traits?
Example: I got this string from Voice Search, 'Set alarm to 5 hours and blahblahblah 1234 etc'.
For this in generic regex I would do this: ([0-9]?).?hours.
But Tasker gives me the result '5 hours' instead of just '5'.

Thanks!

Heather

unread,
Sep 8, 2014, 6:09:29 AM9/8/14
to tas...@googlegroups.com
Sorry to bring up an old topic, but it's exactly what I'm working on. I'm trying to do a Search for a regex capture group, but it puts the whole thing (multiple groups) into the variable. I'm not intending to replace anything, which is where I believe we would use $1 to refer to it. How can I get just the group I want in the variable? I would have thought the variable array would have contained each match, but that is not the case.

Robert Hansen

unread,
Sep 8, 2014, 8:04:54 AM9/8/14
to tas...@googlegroups.com
Capture groups which would be referenced by $1 are only available within the regex engine. You can use them to replace or reorder parts of the search text using the replace option.

Could you give an example of what your search is doing? Perhaps we can help you.

On Mon, Sep 8, 2014 at 3:09 AM, Heather <heathe...@gmail.com> wrote:
Sorry to bring up an old topic, but it's exactly what I'm working on. I'm trying to do a Search for a regex capture group, but it puts the whole thing (multiple groups) into the variable. I'm not intending to replace anything, which is where I believe we would use $1 to refer to it. How can I get just the group I want in the variable? I would have thought the variable array would have contained each match, but that is not the case.

--
You received this message because you are subscribed to the Google Groups "Tasker" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tasker+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/tasker.
For more options, visit https://groups.google.com/d/optout.

easiuser

unread,
Sep 8, 2014, 10:53:34 AM9/8/14
to tas...@googlegroups.com
I just use an inline javascript to get the groups.  Here is a task I call as a subroutine that returns the groups delimited so they can be split easily.  You could just put statement A5 in you task and reference the returned array directly.  Just keep in mind the indexes will be off by 1.
 
GetGroups (61)
 A1: Variable Set [ Name:%expression To:%par1 Do Maths:Off Append:Off ] If [ %par1 Set ]
 A2: Variable Set [ Name:%teststring To:%par2 Do Maths:Off Append:Off ] If [ %par2 Set ]
 A3: Variable Set [ Name:%expression To:\\w* (text|call|sms|phone) \\w*to (\\w+) (.+) Do Maths:Off Append:Off ] If [ %par1 !Set ]
 A4: Variable Set [ Name:%teststring To:send a text to ron hello world Do Maths:Off Append:Off ] If [ %par2 !Set ]
 A5: JavaScriptlet [ Code:var rexp = new RegExp( expression );
var found = [];
found =  rexp.exec( teststring ); Libraries: Auto Exit:On Timeout (Seconds):45 ]
 A6: Flash [ Text:Groups
0 = %found1
1 = %found2
2 = %found3
3 = %found4
4 = %found5 Long:Off ]
 A7: Array Pop [ Variable:%found Position:1 To Var: ]
 A8: Variable Join [ Name:%found Joiner:| Delete Parts:On ]
 A9: Flash [ Text:returning "%found" Long:Off ]
 A10: Return [ Value:%found Stop:On ]

On Monday, September 8, 2014 5:09:29 AM UTC-5, Heather wrote:

Heather

unread,
Sep 8, 2014, 6:11:16 PM9/8/14
to tas...@googlegroups.com
dreddit51, I'm trying to reduce the amount of variable splits I'm using by using regex in the search and replace to search for something *between* string A and string B via lookarounds.

easiest, that looks interesting, I'll have to look at it more tonight when I get home from work. I wonder if it will work for my situation!

Bob Hansen

unread,
Sep 8, 2014, 7:30:59 PM9/8/14
to tas...@googlegroups.com
Do you mean something like this?

string: something between string A and string B

match the word between "string A" and "string B"

This regex will do that:
(?<=string A ).+(?= string B)

If you wanted to only match certain words between string A and string B use:

(?<=string A )and|or|not|this(?= string B)

will match only the words and, or, not, this

Heather

unread,
Sep 8, 2014, 7:35:10 PM9/8/14
to tas...@googlegroups.com

That's what I am doing in most cases, but I have one situation where the before-string has a variable length and variable length lookbehinds are not supported in Tasker's regex (plus I understand they can be time consuming). So that's where my dilemma comes into play! I have a perfect regex that pulls what I need as a group, but Tasker doesn't allow me to pull the group out as a variable, which is what need to do.

Bob Hansen

unread,
Sep 8, 2014, 8:04:26 PM9/8/14
to tas...@googlegroups.com
One way to get only the group value is to match everything in the text being searched; capturing the part you want and replacing the match (everything) with the capture group i.e. $1.

Will that work for you?

Heather

unread,
Sep 8, 2014, 10:05:10 PM9/8/14
to tas...@googlegroups.com

Clever! That may just work! I'll have to try it a little later.

Heather

unread,
Sep 9, 2014, 1:15:43 AM9/9/14
to tas...@googlegroups.com
It worked! Thanks so much, Bob, you're a lifesaver. I'd been driving myself nuts trying to figure that out.
Reply all
Reply to author
Forward
0 new messages