RegEx to Remove All Blank Lines

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3525
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

RegEx to Remove All Blank Lines

Post by JRL » Wed Feb 06, 2013 8:37 pm

I need a way to remove all blank lines from a text list. The following seems to work but is not extremely elegant. Borrowed the Let>pattern=^[\s]+|[\s]+$ line from an adroege post. Figured out the rest from looking on the internet. I found many instances of code that supposedly removes all blank lines but could not find anything I could make work.

Anyone have a single line solution?

Code: Select all

Let>text=%crlf%lineone%crlf%linetwoo%crlf%%crlf%%crlf%linethreee%crlf%%crlf%Linefourrrr%crlf%

Let>pattern=^[\s]+|[\s]+$
RegEx>pattern,text,0,matches,num,1,,text

Let>pattern=[\r\n]+
RegEx>pattern,text,0,matches,num,1,_zKz_,text

StringReplace>text,_zKz_,crlf,text

MessageModal>text

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

Post by jpuziano » Wed Feb 06, 2013 10:20 pm

Hi JRL,

A single line solution *AND* more elegant that what you have there huh...

Not asking for much eh? :roll:

How many points would something like that be worth to you I wonder?

Heh... :twisted:

Oh alright alright... here's the magic one-liner:

RegEx>(?m)^$\x0D\x0A,text,0,matches,num,1,,text

And here it is in a demo macro that shows your text before and after:

Code: Select all

Let>text=%crlf%lineone%crlf%linetwoo%crlf%%crlf%%crlf%linethreee%crlf%%crlf%Linefourrrr%crlf%

MessageModal>text

//discard all blank lines except for one CRLF at the very end if it exists
Let>pattern=(?m)^$\x0D\x0A
RegEx>pattern,text,0,matches,num,1,,text

MessageModal>text
Enjoy!
Last edited by jpuziano on Wed Feb 06, 2013 11:21 pm, edited 2 times in total.
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
JRL
Automation Wizard
Posts: 3525
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Feb 06, 2013 10:42 pm

Very nice... almost. :?

What about that last line still being blank? :shock: The last line is part of "all".

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

Post by jpuziano » Wed Feb 06, 2013 11:16 pm

Hi JRL,

Hmm... all the other lines get to be terminated by CRLF... but you want that last line to be different huh... :lol:

Alrighty then, the pattern has to get a bit more complex, try this...

RegEx>(?m)(^$\x0D\x0A)|(?-m)(\x0D\x0A)+$,text,0,matches,num,1,,text

Here's the demo again...

Code: Select all

Let>text=%crlf%%crlf%%crlf%lineone%crlf%linetwo%crlf%%crlf%%crlf%linethree%crlf%%crlf%Linefour%crlf%%crlf%%crlf%

MDL>text

//discard all blank lines... including any CRLF chars at the very end! 
Let>pattern=(?m)(^$\x0D\x0A)|(?-m)(\x0D\x0A)+$
RegEx>pattern,text,0,matches,num,1,,text

MDL>text
You'll notice I threw in a few more blank lines in the data at the end and the beginning to torture test it...

I think you'll find now that all blank lines are removed... including the final CRLF terminating the last line of the text blob. :D

Enjoy!
Last edited by jpuziano on Wed Feb 06, 2013 11:27 pm, edited 1 time in total.
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
JRL
Automation Wizard
Posts: 3525
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Feb 06, 2013 11:27 pm

Excellent!

You have now provided two fine examples of Regex. One that removes ALL blank lines including the last CRLF.

RegEx>(?m)(^$\x0D\x0A)|(?-m)(\x0D\x0A)+$,text,0,matches,num,1,,text

And one that removes blank lines but leaves the last CRLF.

RegEx>(?m)^$\x0D\x0A,text,0,matches,num,1,,text



I have one question. Is there an advantage using "\x0D\x0A" over using "\r\n" as a representation for CRLF?

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

Post by jpuziano » Wed Feb 06, 2013 11:50 pm

Hi JRL,

Interesting question. I feel the RegEx is taking hold of you...

Well... \r\n is less characters so many would prefer it for that alone.

However I like representing CRLF as \x0D\x0A in a RegEx pattern because:

- Representing a control char using its hex value is more flexible

- Example: I copy some example code that uses \x0D\x0A

- Now I need to target other control chars, simple, just change the numbers

- Had I just used \r\n I may have forgotten the hex format and would have had to look that up again

- Final reason, in a big long ugly RegEx, \x0D\x0A just pops better, just a little faster/easier to understand when looking at it, in my opinion... but it all comes down to personal choice.

Use whatever works best for you and may the RegEx be with you! :lol:
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:45 am

Excellent stuff guys. :)
Phil Pendlebury - Linktree

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