Capture group in RegEx?!

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Niroj@Work
Pro Scripter
Posts: 63
Joined: Thu Dec 10, 2009 8:13 am

Capture group in RegEx?!

Post by Niroj@Work » Fri Dec 18, 2009 12:01 pm

Hi all,

Can you people plz tell me whether RegEx can catch strings using "( )" or not?

code:
Let>exeStep=IGNORE "Error !*Child" PRESS WITHIN 2
RegEx>^\s*IGNORE\s+"([^"]*)"((\s+PRESS\s+\S+)|(\s+ENTER\s+\S+)|(\s+TYPE\s+\S+))\s+WITHIN\s+(\d+)\s*$,%exeStep%,0,MatchMe,cnt,0

Can I get MatchMe_1, MatchMe_2 etc.. for $1, $2 etc.. As in PERL?

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Dec 18, 2009 2:45 pm

Yes, the RegEx command in Macro Scheduler uses (....) to capture groups that can be used in replacement strings as $1,$2,...

But there needs to be a match first.

Make sure that you actually do have a match. Make sure that if you have a comma in your matching pattern that you make that pattern into a variable first, or else Macro Scheduler will see the comma as a delimiter in the RegEx command. (This also applies to the data source that is being searched. I usually make both of them variables like vNeedle and vHaystack.

Using your example (even though there are no commas):

Code: Select all

Let>vNeedle=^\s*IGNORE\s+"([^"]*)"((\s+PRESS\s+\S+)|(\s+ENTER\s+\S+)|(\s+TYPE\s+\S+))\s+WITHIN\s+(\d+)\s*$
Let>vHaystack=%exeStep%

RegEx>%vNeedle%,%vHaystack%,0,MatchMe,cnt,0

It looks like you are searching for strings that start with IGNORE, end with WITHIN and may have PRESS|ENTER|TYPE in the middle with changing parameters for those commands.

I don't have tme to parse your needle right now, but think you could make it easier by eliminating the \s and \d and just using "." instead. Something like this untested example:

Let>vNeedle=^.*IGNORE.*(PRESS|ENTER|TYPE).*WITHIN.*$
Last edited by Bob Hansen on Sun Dec 20, 2009 2:54 am, edited 1 time in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sat Dec 19, 2009 1:39 am

OK, got back to a system with Macro Scheduler.

Using your code sample and my syntax, thie is an example showing you the results of your RegEx using $1..$4 on different lines:

Code: Select all

Let>vNeedle=^.*IGNORE.*"([^"]*)".*(\b(PRESS|ENTER|TYPE)\b.*)WITHIN(.*)$
Let>vHaystack=IGNORE "Error !*Child" TYPE <F1> WITHIN 2
Let>vReplacement=Four Values are:%CRLF%1: $1%CRLF%2: $2%CRLF%3: $3%CRLF%4: $4
RegEx>%vNeedle%,%vHaystack%,0,MatchMe,cnt,1,%vReplacement%,vResult
MessageModal>%vResult%
In this example, the third group value $3, can be ignored. It was needed for the OR option to capture the words. I think you only wanted $1, $2, $4.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Niroj@Work
Pro Scripter
Posts: 63
Joined: Thu Dec 10, 2009 8:13 am

Post by Niroj@Work » Mon Dec 21, 2009 1:41 pm

Thanks Bob for your valuable information.

I used \s,\d etc because I needed it like that..
e.g. I want IGNORE "abcd" not IGNORE123"abcd" ...

Yes I wanted $1, $2 and $4 only. Actually I wanted to catch them outside the RegEx command; but MatchMe_1 or $1 was not working...

Ok I will use replacing method itself..and can later use it.

Thanks..

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts