Regex Replace?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Regex Replace?

Post by Phil Pendlebury » Wed Apr 11, 2012 2:59 pm

Greetings,

I have worked a while on a Regex that will replace a deprecated php command with a new one. This only part of the script but I am stuck on the basic Regex.

What I have is this:

Pattern:

Code: Select all

(.*)(ereg\(")(.*)",(.*)
Replace:

Code: Select all

// $1$2$3$4\n%CRLF%$1preg("/$3/",$4
Basically what this does is looks for a line that contains ereg(" and replaces it with the line itself in comments and then adds preg_replace some delimiters etc.

Example:

Code: Select all

if(ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]{1}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$", $stringtime, $eregidata))
Becomes:

Code: Select all

// if(ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]{1}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$", $stringtime, $eregidata))
if(preg_replace("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]{1}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$/", $stringtime, $eregidata))
This works perfectly if I try it here (with flags for Global and Multiline):

http://gskinner.com/RegExr/

But I cannot for the life of me get it to work in MSched.

Does Msched use the replace variables? ($1$2 etc.)
How would I set the /g/m for global and multiline?

So my script patterns are:

Code: Select all

Let>find_first=/(.*)(ereg\(")(.*)",(.*)
Let>replace_first=// $1$2$3$4\n$1preg("/$3/",$4
And the regex:

Code: Select all

Regex>%find_first%,%search_file_txt%,0,match_arr,num_matches,1,%replace_first%,replaced_txt
I'm wondering if any of you "regexperts" may shine a light on this.

TIA.

P.
Last edited by Phil Pendlebury on Wed Apr 11, 2012 4:43 pm, edited 1 time in total.
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Wed Apr 11, 2012 3:10 pm

I have now got it to work on one line but if I try it on multiple lines it fails.

Is it possible to set the global flags in MS regex?

/(.*)(ereg\(")(.*)",(.*)/g;
Last edited by Phil Pendlebury on Wed Apr 11, 2012 4:48 pm, edited 2 times in total.
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Wed Apr 11, 2012 4:43 pm

Here is test script to give you an idea of what I am after:

Code: Select all

Let>MSG_WIDTH=800
Let>search_file_txt=if(ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]{1}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$", $stringtime, $eregidata))
if(eregi("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]{1}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$", $stringtime, $eregidata))

if(ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]{1}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$", $stringtime, $eregidata))

if(ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})[[:space:]]{1}([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$", $stringtime, $eregidata))


Let>find_first=(.*)(ereg\(")(.*)",(.*)
Let>replace_first=// $1$2$3$4\n%CRLF%$1preg_match("/$3/",$4
Regex>%find_first%,%search_file_txt%,0,match_arr,num_matches,1,%replace_first%,replaced_txt
MDL>Found: %num_matches%%CRLF%%replaced_txt%
As you can see, the replace is only working on the first line.

:-)
Last edited by Phil Pendlebury on Wed Apr 11, 2012 5:05 pm, edited 1 time in total.
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Wed Apr 11, 2012 5:02 pm

OK I needed to read the file into a variable instead of just pasting the lines.

It seems that MS works in Global Mode automatically.

Thanks sorry for confusion.
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Wed Apr 11, 2012 5:08 pm

Gaaah still not working. nm. :oops:
Phil Pendlebury - Linktree

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

Post by jpuziano » Wed Apr 11, 2012 6:16 pm

Hi Phil,

Add this to the start of your regex pattern to turn on multi-line mode: (?m)

Conversely, this would turn it off: (?-m)

You could even turn it on and off multiple times throughout your pattern if that served a purpose... though that's more handy with the switch for case insensitivity (?i)

Check here for more: http://www.regular-expressions.info/modifiers.html

Once you really start taking advantage of regex, there's no going back... enjoy!
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
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Wed Apr 11, 2012 8:28 pm

Thanks buddy but it doesn't seem to work to turn global mode on/off just crashes.

(?g)

I think my regex needs work maybe?
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Wed Apr 11, 2012 9:01 pm

Ok my Regex was dodgy.

This works:

Code: Select all


Let>find_first=([^"\r\n]*.)(ereg\(")([^"\r\n]*.)(",)([^"\r\n]*.)
Let>replace_first=// FOUND EREG $1$2$3$4$5\r%CRLF%$1preg_match("/$3/$4$5
  
Regex>%find_first%,%search_file_txt%,0,match_arr,num_matches,1,%replace_first%,replaced_txt
MDL>Found: %num_matches%%CRLF%%replaced_txt%
Now for the eregi and ereg_replace...

I can see Marcus there saying to himself - Haha Phil is messing with regex, I'll just sit and watch for a bit of entertainment and let him figure it out.

Hehe.

Cheers jpuziano,

P.
Phil Pendlebury - Linktree

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