Can we do a pattern matching for multiple lines using 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

Can we do a pattern matching for multiple lines using RegEx?

Post by Niroj@Work » Mon Mar 15, 2010 6:38 am

Hi All,

Can we use RegEx command for reading & matching for multiple lines?

Like:
The lines are:
{ I am a Boy.
Who r u?
....}

O/p:
"I am a Boy.
Who r u?
...."


In Perl we can use =~ s/pattern/substitute/sg
i.e. we can we use "s" at the end to tell to read for multiple lines.
Can we do the same using RegEx?

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Mar 15, 2010 9:14 am

I may not quite understand what you want to do - Macro Scheduler's RegEx reads across multiple lines without the need to add anything to the pattern.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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

Sorry

Post by Niroj@Work » Mon Mar 15, 2010 10:31 am

:oops:
Sorry, my pattern was wrong ....! I forgot that comma ',' may create prob in pattern in RegEx..

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Mar 15, 2010 5:08 pm

Hi Niroj@Work,

You wanted to use a RegEx> command to turn this:
Input lines wrote:{ I am a Boy.
Who r u?
....}
Into this...
Output lines wrote:"I am a Boy.
Who r u?
...."
Example 1 throws an error and does not run - fair enough

Code: Select all

Let>input={ I am a Boy.%CRLF%Who r u?%CRLF%....}

MDL>input

Let>pattern={|}

RegEx>pattern,input,0,matches,num,1,",output

MDL>output
The above throws an error because the { and } characters are special as they are used to define complex expressions... so we can't use them this way in a Let> command. The way around this problem has traditionally been to use variables, see below.


Example 2 throws two errors but produces correct output anyway - possible bug?

Code: Select all

Let>left_brace={
Let>right_brace=}

Let>input=%left_brace% I am a Boy.%CRLF%Who r u?%CRLF%....%right_brace%

MDL>input

Let>pattern=%left_brace%|%right_brace%

RegEx>pattern,input,0,matches,num,1,",output

MDL>output
Marcus, please try to reproduce. Are these error msgs bugs or are they justified? I tested in MS version 11.1.23 on XP SP3.

Finally, I found a way that works and does not throw errors using the ConCat> command, see below:


Example 3 throws no errors

Code: Select all

Let>left_brace={
Let>right_brace=}

Let>input=%left_brace%
ConCat>input, I am a Boy.%CRLF%Who r u?%CRLF%....
ConCat>input,%right_brace%

MDL>input

Let>pattern=%left_brace%
ConCat>pattern,|
ConCat>pattern,%right_brace%

RegEx>pattern,input,0,matches,num,1,",output

MDL>output
Thanks for posting this question Niroj@Work and take care.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Mar 15, 2010 6:14 pm

Or:

Let>input={"{ I am a Boy." + %CRLF% + "Who r u?" + %CRLF% + "....}"}
MDL>input
Let>pattern={"{|}"}
RegEx>pattern,input,0,matches,num,1,",output
MDL>output
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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