RegEx replace match [UNDOCUMENTED FEATURE?]

Ideas for new features & functions

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 732
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

RegEx replace match [UNDOCUMENTED FEATURE?]

Post by Grovkillen » Mon Jul 12, 2021 3:36 pm

I have a script which is searching through all my source code and compile the documentation based on a LaTeX syntax. In the source code my comments follow a syntax which makes it fairly straight forward to parse it. BUT, if I want to have a function that makes sure I haven't got any UN-escaped LaTeX code/characters I cannot do this with the commands I got at hand using MS. Take this example below:

Code: Select all

LabelToVar>test_string,test_string,1,1,*/
RegEx>(?<!\\)[&%$#\{\}~^],test_string,0,,,1,\<match>,test_string_2

/*
test_string:
this number: #1
this ampersand: &
this percent: %
*/
I want to escape the characters & % $ # { } ~ ^ by adding a back slash to them. But the replace part of the RegEx command will not let me do this. I can only replace the finding with a fixed string i.e.;

Code: Select all

this number: #1
this ampersand: &
this percent: %
will become:

Code: Select all

this number: \1
this ampersand: \
this percent: \
And the StringReplace command will not work either since it will not care if the character already have been escaped, i.e.:

Code: Select all

this number: \#1
this ampersand: &
this percent: \%
will become:

Code: Select all

this number: \\#1
this ampersand: \&
this percent: \\%
Last edited by Grovkillen on Tue Jul 13, 2021 3:57 pm, edited 1 time in total.
Let>ME=%Script%

Running: 15.0.19b
version history

User avatar
Grovkillen
Automation Wizard
Posts: 732
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: RegEx replace match

Post by Grovkillen » Mon Jul 12, 2021 5:27 pm

I see now that the replace part of regex does in fact allow for regex syntax:

Code: Select all

LabelToVar>test_string,test_string,1,1,*/
RegEx>(?<!\\)[&%$#\{\}~^],test_string,0,,,1,\test,test_string_2
RegEx>(?<!\\)[&%$#\{\}~^],test_string,0,,,1,\\test,test_string_3
RegEx>(?<!\\)[&%$#\{\}~^],test_string,0,,,1,\r,test_string_4
RegEx>(?<!\\)[&%$#\{\}~^],test_string,0,,,1,{$110},test_string_5
/*
test_string:
this number: #1
this ampersand: &
this percent: %
*/
test_string_2:

Code: Select all

this number: 	est1
this ampersand: 	est
this percent: 	est
test_string_3:

Code: Select all

this number: \test1
this ampersand: \test
this percent: \test
test_string_4:

Code: Select all

this number: 
1
this ampersand: 

this percent: 

test_string_5:

Code: Select all

this number: 2721
this ampersand: 272
this percent: 272
Maybe someone could shed some light here?
Let>ME=%Script%

Running: 15.0.19b
version history

User avatar
Grovkillen
Automation Wizard
Posts: 732
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: RegEx replace match

Post by Grovkillen » Tue Jul 13, 2021 3:55 pm

I have now read up on the regex syntax and my previous pattern didn't include a capturing group.
I.e. this
(?<!\\)[&%$#\{\}~^] needed to be this
(?<!\\)([&%$#\{\}~^])

Code: Select all

LabelToVar>test_string,test_string,1,1,*/
RegEx>(?<!\\)([&%$#\{\}~^]),test_string,0,,,1,\\$1,test_string_2
MDL>test_string_2
/*
test_string:
this number: #1
this ampersand: &
this percent: %
*/
It's working!

Code: Select all

this number: \#1
this ampersand: \&
this percent: \%
This is a really powerful feature which isn't documented as far as I can tell.
Let>ME=%Script%

Running: 15.0.19b
version history

User avatar
Grovkillen
Automation Wizard
Posts: 732
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: RegEx replace match [UNDOCUMENTED FEATURE?]

Post by Grovkillen » Tue Jul 13, 2021 4:05 pm

As an example of a really powerful way of adding strings INBETWEEN two capturing groups:

Code: Select all

LabelToVar>text_string,text_string,1,1,*/
RegEx>(Grovkillen|Allan)(!),text_string,0,,,1,$1..ABC123..$2,text_string_2
MDL>text_string_2
/*
text_string:
Are you Allan?
Allan!? My name is Grovkillen!
Aha, Grovkillen... got ya!
*/
Output:

Code: Select all

Are you Allan?
Allan..ABC123..!? My name is Grovkillen..ABC123..!
Aha, Grovkillen... got ya!
Let>ME=%Script%

Running: 15.0.19b
version history

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