RegEx-Removal of Leading & Trailing Spaces
Moderators: Dorian (MJT support), JRL
RegEx-Removal of Leading & Trailing Spaces
How to remove all leading and trailing spaces in a multiple-line variable, say, 'Text', with RegEx> command?
/*
FullText:
.......................Line 1
....................Line 2
..................Line3
*/
LabelToVar>FullText,Text
MDL>Text
Those dots represents spaces.
/*
FullText:
.......................Line 1
....................Line 2
..................Line3
*/
LabelToVar>FullText,Text
MDL>Text
Those dots represents spaces.
-
- Pro Scripter
- Posts: 60
- Joined: Tue Dec 22, 2009 9:51 am
- Location: Mannheim ( Germany )
- Contact:
I think this is what you are looking for.
Kind regards
Oliver Hilger
Code: Select all
/*
FullText:
.......................Line 1
....................Line 2
..................Line 3
*/
LabelToVar>FullText,strText
// Before
MDL>strText
// now i removed all dots ....you can Change that to {" "} below for spaces
Let>pattern={"."}
Regex>pattern,strText,1,ArrMatches,NumMatches,1,,strTextoutput
// After replace
MDL>strTextoutput
Oliver Hilger
Oliver Hilger Mannheim
alias Olllllliii
alias Olllllliii
Oliver,
Thanks for your help.
I notice you're using easy pattern.
I use the following RegEx to accomplish the same task:
RegEx>[oneormore space],ClipText,1,Matches,num,1,,ClipText
Alas, nonetheless, having tested million times, I confirm RegEx is troublesome as to Unicode support. Now I use time-honored StringReplace.
StringReplace doesn't corrupt Unicode chars.
BTW, are you from Italy?
Thanks again.
Best regards,
Armstrong
Thanks for your help.
I notice you're using easy pattern.
I use the following RegEx to accomplish the same task:
RegEx>[oneormore space],ClipText,1,Matches,num,1,,ClipText
Alas, nonetheless, having tested million times, I confirm RegEx is troublesome as to Unicode support. Now I use time-honored StringReplace.
StringReplace doesn't corrupt Unicode chars.
BTW, are you from Italy?
Thanks again.
Best regards,
Armstrong
-
- Pro Scripter
- Posts: 60
- Joined: Tue Dec 22, 2009 9:51 am
- Location: Mannheim ( Germany )
- Contact:
Hi ,
yes ! there is a second way with stringreplace.
I Think this is faster too.
I will check the Speed of both with a big file...
testing now ...results will come soon ...
No , i am not from Italy ...i am from Germany ( west )...Mannheim thats
near Heidelberg.
yes ! there is a second way with stringreplace.
I Think this is faster too.
Code: Select all
/*
FullText:
.......................Line 1
....................Line 2
..................Line 3
*/
LabelToVar>FullText,strText
// Before
MDL>strText
Let>pattern={"."}
Stringreplace>strText,%pattern%,,strText
// After
MDL>strText
testing now ...results will come soon ...
No , i am not from Italy ...i am from Germany ( west )...Mannheim thats
near Heidelberg.
Oliver Hilger Mannheim
alias Olllllliii
alias Olllllliii
>.i am from Germany ( west )...Mannheim thats
Sorry for my poor observation. You must be a banker/financer.
Let's get back to Macro Scheduler.
So far, StringReplace is my most favorable tool to replace chars because it never corrupt any unicode chars in any languages. But I have to pay a price for the safety. A single RegEx could have accomplished the following verbose code:
SRT>DelExcessChars
/* Delete excessive chars-tabs, blank lines, leading spaces */
Let>Pattern1=%CRLF%%CRLF%%CRLF%
Let>Pattern2=%Space%%Space%
Let>Pattern3=%CRLF%%Space%
/* Delete tabs */
Label>DelTab
If>{Pos(%Tab%,%Cliptext%)>0}
Stringreplace>cliptext,%Tab%,,Cliptext
Goto>DelTab
Endif
Label>DelCRLF
If>{pos(%Pattern1%,%ClipText%)>0}
StringReplace>ClipText,%Pattern1%,%CRLF%,ClipText
Goto>DelCRLF
Endif
/* Delete extra spaces */
Label>DelSpace
If>{pos(%Pattern2%,%ClipText%)>0}
StringReplace>ClipText,%Pattern2%,%Space%,ClipText
Goto>DelSpace
Endif
/* Delete leading spaces */
Label>DelLeadSpace
If>{pos(%Pattern3%,%ClipText%)>0}
StringReplace>ClipText,%Pattern3%,%CRLF%,ClipText
Goto>DelLeadSpace
Endif
End>DelExcessChars
Sorry for my poor observation. You must be a banker/financer.
Let's get back to Macro Scheduler.
So far, StringReplace is my most favorable tool to replace chars because it never corrupt any unicode chars in any languages. But I have to pay a price for the safety. A single RegEx could have accomplished the following verbose code:
SRT>DelExcessChars
/* Delete excessive chars-tabs, blank lines, leading spaces */
Let>Pattern1=%CRLF%%CRLF%%CRLF%
Let>Pattern2=%Space%%Space%
Let>Pattern3=%CRLF%%Space%
/* Delete tabs */
Label>DelTab
If>{Pos(%Tab%,%Cliptext%)>0}
Stringreplace>cliptext,%Tab%,,Cliptext
Goto>DelTab
Endif
Label>DelCRLF
If>{pos(%Pattern1%,%ClipText%)>0}
StringReplace>ClipText,%Pattern1%,%CRLF%,ClipText
Goto>DelCRLF
Endif
/* Delete extra spaces */
Label>DelSpace
If>{pos(%Pattern2%,%ClipText%)>0}
StringReplace>ClipText,%Pattern2%,%Space%,ClipText
Goto>DelSpace
Endif
/* Delete leading spaces */
Label>DelLeadSpace
If>{pos(%Pattern3%,%ClipText%)>0}
StringReplace>ClipText,%Pattern3%,%CRLF%,ClipText
Goto>DelLeadSpace
Endif
End>DelExcessChars
Hi armsys,
Here are a few methods, all using RegEx. This first one uses two separate RegEx commands, one to trim leading spaces on the lines within the variable... and a second RegEx command to trim trailing spaces.
Next here's a different approach in which we use only one RegEx command and manage to trim both leading and trailing spaces on text lines within a variable... by taking advantage of grouping i.e. backreferences. We actually match each complete line in 3 separate parts, the leading spaces, the stuff in the middle and the trailing spaces... then we replace the whole line with just the stuff in the middle, effectively getting rid of the leading and trailing spaces.
In either of the above examples, we are just trimming spaces, but you can easily replace the space char in the patterns above with the following character class:
[ /t]
There is a single space before the / slash above. This allows us to match both spaces and tabs.
The simplest approach of all though might be the one below. This combines two patterns, one to match spaces or tabs at the front of the lines... and another to match spaces or tabs at the end of the lines... using the alternation operator | which is the vertical bar or pipe symbol. And since we can match them, we can also replace them with nothing... to remove them... here we go.
I hope this was helpful... take care.
Here are a few methods, all using RegEx. This first one uses two separate RegEx commands, one to trim leading spaces on the lines within the variable... and a second RegEx command to trim trailing spaces.
Code: Select all
/*
FullText:
Line 1
Line 2
Line 3
*/
LabelToVar>FullText,strText
//lines have leading and trailing spaces
MDL>strText
//Strip leading spaces from all lines
//(?m) turns on multi-line mode
Let>pattern=(?m)^ +
RegEx>pattern,strText,0,matches,num,1,,strText
//lines no longer have leading spaces
MDL>strText
//Strip trailing spaces from all lines
//(?m) turns on multi-line mode
Let>pattern=(?m) +$
RegEx>pattern,strText,0,matches,num,1,,strText
//lines no longer have leading or trailing spaces
MDL>strText
Code: Select all
/*
FullText2:
Line 1
Line 2
Line 3
*/
LabelToVar>FullText2,strText2
//lines have leading and trailing spaces
MDL>strText2
//Strip leading and trailing spaces from all lines
//(?m) turns on multi-line mode
Let>pattern=(?m)(^ +)(.*?)( +$)
RegEx>pattern,strText,0,matches,num,1,$2,strText2
//lines no longer have leading or trailing spaces
MDL>strText2
[ /t]
There is a single space before the / slash above. This allows us to match both spaces and tabs.
The simplest approach of all though might be the one below. This combines two patterns, one to match spaces or tabs at the front of the lines... and another to match spaces or tabs at the end of the lines... using the alternation operator | which is the vertical bar or pipe symbol. And since we can match them, we can also replace them with nothing... to remove them... here we go.
Code: Select all
/*
FullText3:
Line 1
Line 2
Line 3
*/
LabelToVar>FullText3,strText3
//lines have leading and trailing spaces
MDL>strText3
//let's add some tabs for fun
StringReplace>strText3,Line,%TAB%Line%TAB%,strText3
//lines have leading and trailing spaces and tabs
MDL>strText3
//Strip leading and trailing spaces and tabs from all lines
//(?m) turns on multi-line mode
Let>pattern=(?m)^[ \t]+|[ \t]+$
RegEx>pattern,strText,0,matches,num,1,,strText3
//lines no longer have leading or trailing spaces or tabs
MDL>strText3
Last edited by jpuziano on Fri Apr 05, 2013 3:22 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 -
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 -