RegEx Replacement when only match of pos (ie no char)

Ideas for new features & functions

Moderators: Dorian (MJT support), JRL

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

RegEx Replacement when only match of pos (ie no char)

Post by hagchr » Thu Sep 24, 2015 10:31 am

Hi, I once raised this as a technical question but never as an enhancement suggestion:

viewtopic.php?f=2&t=8297

Not sure of difficulty, but one can wish anything, right? Not sure from when, but later versions of Perl (at least 5.18 not sure of earlier) seem to support it.

Small example to illustrate. Some text with different indentations and I want to adjust all to be indented just 1 tab. Since current version does not replace where there is a match of position but no characters, it will not replace in line 3.

Code: Select all

LabelToVar>Data,source
Let>tmp0=(?m-s)^([ ]|\t|$)*
RegEx>tmp0,source,0,m,nm,1,\t,res
MDL>res
/*
Data:
   Some text
        Some text
Some text
              Some text 
    Some text
*/

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: RegEx Replacement when only match of pos (ie no char)

Post by armsys » Sat Oct 03, 2015 11:00 pm

Hope you find the following code helpful:

Code: Select all

LabelToVar>Data,source
Let>tmp0=(?m-s)^(?=\s)([ ]|\t|$)*
RegEx>tmp0,source,0,m,nm,1,\t,res
MDL>res
Let>tmp0=(?m-s)^(?=\S)(.*)$
RegEx>tmp0,res,0,m,nm,1,\t$1,res
MDL>res
/*
Data:
   Some text
        Some text
Some text
              Some text
    Some text
*/

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: RegEx Replacement when only match of pos (ie no char)

Post by armsys » Sun Oct 04, 2015 12:19 am

A more compact version:

Code: Select all

LabelToVar>Data,source
Let>tmp0=(?m-s)^\s*(?=\S)(.*)$
RegEx>tmp0,source,0,m,nm,1,\t$1,res
MDL>res
/*
Data:
   Some text
        Some text
Some text
              Some text
    Some text
*/

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

Re: RegEx Replacement when only match of pos (ie no char)

Post by hagchr » Sun Oct 04, 2015 11:18 am

Hi, Many thanks, I will use that. There would still be a problem if you say have some non-text lines that you want to keep with adjusted format (would be removed here) but if that happens I will try to handle that separately.

Code: Select all

LabelToVar>Data,source
Let>tmp0=(?m-s)^\s*(?=\S)(.*)$
RegEx>tmp0,source,0,m,nm,1,\t$1,res
MDL>res

/*
Data:
   Some text
        Some text
Some text
        

              Some text
    Some text
*/

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: RegEx Replacement when only match of pos (ie no char)

Post by armsys » Sun Oct 04, 2015 2:27 pm

hagchr wrote:Hi, Many thanks, I will use that. There would still be a problem if you say have some non-text lines that you want to keep with adjusted format (would be removed here) but if that happens I will try to handle that separately.
Do you mean you want to keep the blank lines just like below?

Code: Select all

   Some text
   Some text
   Some text
     
   
   Some text
   Some text

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

Re: RegEx Replacement when only match of pos (ie no char)

Post by hagchr » Sun Oct 04, 2015 3:25 pm

Right, that the empty lines would be indented the same way, ie 1 tab in this case.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: RegEx Replacement when only match of pos (ie no char)

Post by armsys » Mon Oct 05, 2015 12:11 am

Marcus,
I check with other regex tools and confirm my regex works to meet hgchr's requirement.
Namely, my regex and replacement pattern can preserve blank lines, with spaces or otherwise.
Is this an internal bug of MS to remove all blank lines?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: RegEx Replacement when only match of pos (ie no char)

Post by armsys » Mon Oct 05, 2015 6:39 am

Hi Hgchr,
After hundred attempts for 3 hours, I make it. Hope it helps.

Code: Select all

LabelToVar>Data,source
Let>regex=(?m-s)^[ \t]*([A-Za-z ]*)$
RegEx>regex,source,0,m,nm,1,\t$1,res
MDL>res
/*
Data:
   Some text
        Some text
Some text
        
       
              Some text
    Some text
*/

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

Re: RegEx Replacement when only match of pos (ie no char)

Post by hagchr » Mon Oct 05, 2015 7:08 am

Hi armsys (or RegEx Ninja). Many thanks. If I take your pattern and use it eg in RegexBuddy then it will indent everything perfectly also if you add some empty lines (ie only CRLF). In MS, if there is no character match then it does not replace, which means any empty lines would not be adjusted, which leads to the enhancement suggestion that it would be great if there is a replacement also when there is not a character match but a position match (between ^ and $ in your pattern). Your code is perfect for what I am doing so many thanks again.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: RegEx Replacement when only match of pos (ie no char)

Post by armsys » Mon Oct 05, 2015 7:28 am

You're welcome, my friend.

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