Easy Pattern syntax for Groups

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
obfusc88
Pro Scripter
Posts: 85
Joined: Wed Mar 14, 2007 6:22 pm

Easy Pattern syntax for Groups

Post by obfusc88 » Thu Mar 04, 2021 1:26 am

I have been spending hours with different versions of syntax to figure out how to extract a group using RegEx. I have no problems with other "flavors" of RegEx in other editors. I have come to the conclusion that Macro Scheduler does not do that without using Easy Pattern syntax. I have used Macro Scheduler for years (using 14.5.7) and never needed to learn Easy Pattern before.

So, I jumped in and learned a number of useful expressions, especially[Drive],[FileName],[Path], [EmailAddress], etc., but I cannot seem to figure out how to get a group. And since I can't get a group, then I can't do a Replace.

So, I have provided some code samples. The first Replace function works fine with normal RegEx.
Can some one please provide the correct syntax for the vNeedles in the other three samples?
I know these simple examples can probably be done withMidStr and Position commands, but I am trying to understand the Easy Pattern syntax, so I thought if I understood these, then I can take it from there.
I am having trouble with placement of the [braces], (parentheses), the phrase [capture(...)], with a trailing comma and [capture(...) as 'varname' ] without the trailing comma after the closing brace "],"

I am sure my problem is defining vNeedle, but I need some help.
It looks like the captured group can be referred to [groupx] or as $x using 1-9 or a-z?
I am looking for a few examples of using captured groups as variables, and as replacement values.

============================================

Code: Select all

//Replace example Normal RegEx (Works OK)
Let>vHaystack=Today's magic word is ~FRED~, changed from MARY.
Let>vNeedle=~.*~
RegEx>vNeedle,vHaystack,0,vNeedleNo,vTotalNeedles,1,~SAM~,vFinalHaystack
MessageModal> Hope to see%CRLF%"Today's magic word is ~SAM~, changed from MARY".
MessageModal>New line is %vFinalHaystack%

//Replace example EasyPattern (Does not work)
Let>vHaystack=Today's magic word is ~FRED~, changed from MARY.
Let>vNeedle=~[capture(~ .*~)], ; Grab everything between tildes(~)
RegEx>vNeedle,vHaystack,1,vNeedleNo,vTotalNeedles,1,~SAM~,vFinalHaystack
MessageModal> Hope to see%CRLF%"Today's magic word is ~SAM~, changed from MARY".
MessageModal>New line is %vFinalHaystack%

//Get Group example EasyPattern (Does not exist)
//Macro Scheduler Normal Regex, does not seem to handle groups,MUST use Easy Pattern

//Get Group example EasyPattern (Does not work)
Let>vHaystack=abcdxyz~this is what I want~xyz987~along with this~ too
Let>vNeedle=~[capture(.*)],~ ; Grab everything between tildes
RegEx>vNeedle,vHaystack,1,vNeedleNo,vTotalNeedles,0,,
MessageModal> Hope to see%CRLF%"this is what I want"%CRLF% and%CRLF%"along with this"
MessageModal>Found %vTotalNeedles% matches.%CRLF%First match is %vNeedleNo_1%%CRLF%Second match is %vNeedleNo_2%..

EXIT>
Thanks for listening.

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

Re: Easy Pattern syntax for Groups

Post by Bob Hansen » Fri Mar 05, 2021 3:13 am

You noted:
//Macro Scheduler Normal Regex, does not seem to handle groups,MUST use Easy Pattern
I don't use Easy Pattern, but I think you are wrong about groups in normal RegEx. If I understand your question, the fourth element of the RegEx expression, "Match Array", contains the groups.

But instead of using something like $1 or $5 to grab those groups, you get them from the array as "MatchArray_1" and "Match_Array_5".

This works OK for single groups and multiple finds, but multiple groups can be more difficult. I have handled multiple groups in the same pattern with multiple RegEx expressions and then do some string manipulation with the results from each array. There may be an easier way, but it has been a Sensible Solution for me.

But your real question is using "Easy Pattern", I have no answer, never needed to use it.

Good luck.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Easy Pattern syntax for Groups

Post by hagchr » Sun Mar 07, 2021 10:52 am

I agree with Bob, normal RegEx handles groups etc, there is no need to go to for EasyPattern just for that. Actually, personally, I find the normal RegEx (much) easier and like Bob never bothered with EasyPattern.

Out of curiosity I looked at your questions and if you are just looking for the matches then no need for capturing groups at all. As Bob says, just pull the matches from "MatchArray_1" etc and put together / construct your new string etc. If you want to work with capturing groups I tried to adjust your examples to illustrate (in terms of pattern you need to use either, EasyPattern or Normal RegEx, not mix):

Code: Select all

// Ex 2
// vNeedle needs to use EasyPattern language 
Let>vHaystack=Today's magic word is ~FRED~, changed from MARY.
Let>vNeedle=~[capture(1+ letters)]~
RegEx>vNeedle,vHaystack,1,vNeedleNo,vTotalNeedles,1,~SAM~ ($1 was removed),vFinalHaystack
MDL>vFinalHaystack
//
//
// Ex 3
// Here you have one group but multiple (two) matches. There is just ONE group 
// so you cannot address the matches separately through the groups. You can eg
// do the same thing with each match, eg add "" around them.
// There seems to be a bug in the program - if I am not mistaken the pattern should
// use "shortest" and not "longest" which was needed here to get the right answer.  
Let>vHaystack=abcdxyz~this is what I want~xyz987~along with this~ too
Let>vNeedle=~[capture(longest 1+ characters)]~
RegEx>vNeedle,vHaystack,1,vNeedleNo,vTotalNeedles,1, "$1" ,res
MDL>res
//
//
// Bonus example - normal RegEx with capturing groups
// Use of two capturing groups to be able to use matches in separate replacements.
Let>OldString=Today's magic word is ~FRED~, changed from ~MARY~.
Let>newName=BOB
Let>replacementString=Today's magic word is %newName%%COMMA% changed from $1 ($2 was removed)
//
Let>pattern=.+~(.+?)~.+~(.+?)~
RegEx>pattern,oldString,0,m,nm,1,replacementString,res
MDL>res
//
// Bonus example - normal RegEx without capturing groups
// Just use the normal matches to piece together the result.
Let>OldString=Today's magic word is ~FRED~, changed from ~MARY~.
Let>newName=BOB
//
Let>pattern=~.+?~
RegEx>pattern,oldString,0,m,nm,0
Let>res=Today's magic word is %newName%%COMMA% changed from %m_1% (%m_2% was removed)
StringReplace>res,~,,res
MDL>res
Hope it helps.

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