Using Regex in Search and 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:

Using Regex in Search and Replace

Post by Phil Pendlebury » Fri Feb 01, 2013 7:07 pm

This is a question about using the MS Search and Replace feature.

To put it simply I am trying to add ",CCOEFF" to all lines that use:

"FindImagePos>"

So for example:

Code: Select all

FindImagePos>blah.bmp,glah.bmp,%tol%,1,XArr,YArr,numfound
Should become:

Code: Select all

FindImagePos>blah.bmp,glah.bmp,%tol%,1,XArr,YArr,numfound,CCOEFF
I have written some (probably very rubbish) Regex that works fine in Notepad++

In Search:
(FindImagePos>.+?,.+?,.+?,.+?,.+?,.+?,.+)

In Replace:
\1,CCOEFF

However in MS Search Replace the whole string is replaced with \1,CCOEFF

Is there something wrong here or does MS Search/Replace not support Regex Replace?

Of course I can copy the scripts to notepad++ and do the Search / Replace and then copy back to MS but I thought it may be of interest to post here. And I would like to keep the script within MS if possible.

Also I think this may be useful for any other users who have moved to MS14 with the new CCOEFF feature.

Any thoughts?

Cheers,

P
Phil Pendlebury - Linktree

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

Post by Phil Pendlebury » Mon Feb 04, 2013 10:57 am

No takers on this one?
Phil Pendlebury - Linktree

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

Post by Marcus Tettmar » Mon Feb 04, 2013 12:58 pm

I need to check this when I'm back in the office. Not sure if this is possible or what the syntax is if it is but will try it.

If it doesn't work you could always use Notepad++ or similar and could also do it across multiple files if needed that way.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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

Post by Phil Pendlebury » Mon Feb 04, 2013 1:36 pm

Marcus Tettmar wrote:I need to check this when I'm back in the office. Not sure if this is possible or what the syntax is if it is but will try it.

If it doesn't work you could always use Notepad++ or similar and could also do it across multiple files if needed that way.
Yup OK Marcus thanks for your reply - Just fyi as mentioned:
Of course I can copy the scripts to notepad++ and do the Search / Replace and then copy back to MS but I thought it may be of interest to post here. And I would like to keep the script within MS if possible.
:-)
Phil Pendlebury - Linktree

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

Post by Marcus Tettmar » Mon Feb 04, 2013 1:46 pm

I don't think the replace field accepts regex so I don't think you can do what you want, sorry.
Phil Pendlebury wrote:Of course I can copy the scripts to notepad++ and do the Search / Replace and then copy back to MS but I thought it may be of interest to post here. And I would like to keep the script within MS if possible.
Why copy, paste and paste back? Macro Scheduler scripts are just .scp files. Point Notepad++ at them.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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

Post by Phil Pendlebury » Mon Feb 04, 2013 1:56 pm

I don't think the replace field accepts regex so I don't think you can do what you want, sorry.
It is absolutely fine, as I am not a regex expert I wasn't sure I may have been doing something wrong.
Why copy, paste and paste back? Macro Scheduler scripts are just .scp files. Point Notepad++ at them.
Hehe right on. :-)

At the end of the day with this I will probably do it manually anyway as there may be areas I want to keep with the "old" method.

:-)
Phil Pendlebury - Linktree

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

Post by jpuziano » Thu Feb 07, 2013 6:00 am

Phil Pendlebury wrote:No takers on this one?
Hi Phil,

Interesting...

Yes it looks like in the MS Editor, using the Search/Replace feature, you can use a RegEx expression in the search field but not in the Replace field.

Specifically, the replace field will not recognize /1 as your first backreference which holds the matched line of interest.

I tried $1 as well for the backreference and it didn't work either so...

Marcus:

Q1) Can the Replace field be enhanced so that it does understand a backreference when Options/Regular expressions is checked?

Q2) In the Search/Replace Dialog, under Options, there is a checkbox called "Replace with template" which is greyed out if "Regular expressions" is not checked. What is that and how do we use it? I could not find anything on it in the Help File by searching for the word "template".

Ahh well... until then Phil... you can certainly do this with a script.

See below and enjoy!

Code: Select all

LabelToVar>LINES,myLINES

/*
LINES:
FindImagePos>blah.bmp,glah.bmp,%tol%,1,XArr,YArr,numfound
  Another one of Phil's script lines
  FindImagePos>blah.bmp,glah.bmp,%tol%,1,XArr,YArr,numfound
    Yet another one of Phil's script lines
    FindImagePos>blah.bmp,glah.bmp,%tol%,1,XArr,YArr,numfound
*/

//For your viewing pleasure - just makes the message box a little wider 
Let>MSG_WIDTH=425

MDL>myLINES

//We will add this text onto the end of each of our target lines.
//This allows us to "bury" the , which would otherwise confuse
//the RegEx line below because commas are used to separate parameters.
Let>add_on=,CCOEFF

//See: http://www.regular-expressions.info/brackets.html
//Round Brackets in your RegEx pattern create a Backreference we can take advantage of later
//text matched inside first set of (round brackets) is stored in Backreference $1
//text matched inside second set of (round brackets) is stored in Backreference $2 and so on...
//The first set of (round brackets) in pattern do not create a backreference, (?m) just turns multiline mode on
//Our target text line will be captured into backreference $1
Let>pattern=(?m)^( *FindImagePos>[^\x0D\x0A]*)

//Now when we replace, we replace with $1 and %add_on% right after it 
RegEx>pattern,myLINES,0,matches,num,1,$1%add_on%,myLINES

MDL>myLINES
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 » Thu Feb 07, 2013 9:32 am

Hi jpuziano,

Glad you found this of interest.

I actually took Marcus' idea yesterday and just opened the script in Notepad++ it worked almost perfectly.

I did not see your script idea until just now but I did indeed think of writing a script to do it but for this purpose (I even thought about writing a whole Search & Replace script to be used anywhere) but it would have been overkill and nothing more than what Notepad++ or GREP can already do.

I did end up having the replace line as "\1,CCOEFF\n" as for some reason the original line was adding the ",CCOEFF" but also stripping the line break.

Ideally I would have liked to have done a lot more:

Replaced the original line with a COPY of itself
Then added ",CCoEFF"
Commented out the original line
Changed one of the variables (the one that points to tolerance)

So:

Code: Select all

FindImagePos>blah.bmp,glah.bmp,%tol%,1,XArr,YArr,numfound
Becomes:

Code: Select all

// FindImagePos>blah.bmp,glah.bmp,%tolerance%,1,XArr,YArr,numfound
FindImagePos>blah.bmp,glah.bmp,%coeffient%,1,XArr,YArr,numfound,CCOEFF

But time was not on my side. I will do this manually later.

Cheers guys,
Last edited by Phil Pendlebury on Sat Feb 16, 2013 6:18 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 » Sat Feb 16, 2013 5:56 pm

And for the sake of continuity I will add...

I then wanted to change the script again to give the option to use old or new method - including 2 different tolerance models:

From:

Code: Select all

FindImagePos>%MppData%\graphics\%gfx_folder%\%folderopengraf%,%MAppData%\screen\export%tc6%.bmp,%deftolerance%,1,arrXarr,arrYarr,c6_folderopen,CCOEFF

Desired end result:

Code: Select all

IF>matchtype=COEF
  FindImagePos>%MAppData%\graphics\%gfx_folder%\%folderopengraf%,%MAppData%\screen\export%tc6%.bmp,%defFcoeftol%,1,arrXarr,arrYarr,c6_folderopen,CCOEFF
ENDIF
IF>matchtype=ORIG
  FindImagePos>%MAppData%\graphics\%gfx_folder%\%folderopengraf%,%MppData%\screen\export%tc6%.bmp,%defForigtol%,1,arrXarr,arrYarr,c6_folderopen
ENDIF

So...

Search:

Code: Select all

(FindImagePos>)(.+?),(.+?),(%deftolerance%),(.+?),(.+?),(.+?),(CCOEFF)
Replace:

Code: Select all

IF>matchtype=COEF\n  \1\2,\3,%defNcoeftol%,\5,\6,\7,\8\nENDIF\nIF>matchtype=ORIG\n  \1\2,\3,%defNorigtol%,\5,\6,\7\nENDIF\n
And so on... worked an absolute charm!

REGEX Rocks eh.
Last edited by Phil Pendlebury on Wed Feb 20, 2013 7:47 pm, edited 1 time in total.
Phil Pendlebury - Linktree

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

Post by jpuziano » Sat Feb 16, 2013 6:01 pm

Phil Pendlebury wrote:REGEX Rocks eh.
Indeed it does Phil... :D indeed it does.
jpuziano earlier in this thread wrote: Q1) Marcus, can the Replace field be enhanced so that it does understand a backreference when Options/Regular expressions is checked?

Q2) Marcus or anyone - In the Search/Replace Dialog, under Options, there is a checkbox called "Replace with template" which is greyed out if "Regular expressions" is not checked. What is that and how do we use it? I could not find anything on it in the Help File by searching for the word "template".
Marcus, anything on the questions above? Or if anyone out there knows anything about 2) "Replace with template" please let us know.

Thanks 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: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Feb 20, 2013 3:29 pm

Re both questions I will need to ask the developers of the editing component. I have no idea what the template thing is either. I will also need to refer back to the syntax editor developers.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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

Post by jpuziano » Wed Feb 20, 2013 4:29 pm

Marcus Tettmar wrote:Re both questions I will need to ask the developers of the editing component. I have no idea what the template thing is either. I will also need to refer back to the syntax editor developers.
OK, thanks for the reply. Please let us know when you find out more.

Maybe that template thing is something really useful but we won't know until we learn how to use it...

Thanks again 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: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Mar 12, 2013 11:44 am

Phil,

I know this is probably a bit late, but I have just realised that all you needed to do was check off "Replace with Template" as well as "Regular Expressions" and then the way you were doing your regex search and replace would have worked:

So:
1. Check "Regular Expressions"
2. Check "Replace with Template"

In Text to find enter:
(FindImagePos>.+?,.+?,.+?,.+?,.+?,.+?,.+)

In Replace with, enter:
\1,COEFF

And that would work. The missing link was "Replace with Template" which allows the \1 to be used to represent the value in the expression's back reference.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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

Post by Phil Pendlebury » Tue Mar 12, 2013 11:56 am

Thanks Marcus, good to know. I will run some tests with that soon.

My Search / Replace ended up getting quite complex as you may have seen.

I ended up replacing the original replace (which added COEFF) with one that found the newly replaced line and replaced that with the old line and another new line wrapped in IF>THEN (to put it simply).

I still ended up checking it manually... :-)

The Regex was a bit scrappy but getting the line layout correct was tricky.
Phil Pendlebury - Linktree

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

Post by jpuziano » Tue Mar 12, 2013 5:14 pm

Marcus Tettmar wrote:Phil,

I know this is probably a bit late, but I have just realised that all you needed to do was check off "Replace with Template" as well as "Regular Expressions" and then the way you were doing your regex search and replace would have worked:

So:
1. Check "Regular Expressions"
2. Check "Replace with Template"

In Text to find enter:
(FindImagePos>.+?,.+?,.+?,.+?,.+?,.+?,.+)

In Replace with, enter:
\1,COEFF

And that would work. The missing link was "Replace with Template" which allows the \1 to be used to represent the value in the expression's back reference.
Its never too late, thanks.

So in the MS Editor, when you use the Search/Replace feature...

"Replace with Template" really means, "Replace allowing RegEx Back References \1, \2, \3 etc."... ?

Too bad it doesn't say that or something close... because then we'd have guessed what it was for.

Can you change the text label on that dialog Marcus?

It would also be nice to get a new topic added to the Help File to let users know how to use the MS Editor's search and replace feature to full advantage... when the Search/Replace dialog comes up, under Options: checking off "Regular expressions" and also checking off "Replace with Template" to take advantage of Back References in a complex Search and Replace operation. In fact, this very example of Phil's could be used as the basis for the example.

Thanks for sharing this with us Marcus and... for considering a new topic for the Help File.

Hey, and thanks Phil for posting your experiences here... look at the Search and Replace power that you have eventually caused to be "discovered" and shared here on the forums!
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 - :-)

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