RegEx Replace Text -- strip leading and trailing spaces
Wouter
wouter.abraham at scarlet.be
Thu Apr 14 04:52:50 EDT 2005
On 14 Apr 2005, at 01:49, Ken Ray wrote:
> On 4/13/05 2:47 PM, "Trevor DeVore" <lists at mangomultimedia.com> wrote:
>
>> get matchText(pString, "(?s)^[ \t\r\n]+(.*?)[ \t\r\n]+$", tReturnVal)
>>
>> This will strip leading and trailing whitespace from a multiline
>> string. But given that you say that word 1 to -1 is that much faster
>> I
>> am going to switch to that. Those of us still trying to break away
>> from our previous languages like to complicate things on occasion ;)
>
> Unfortunately neither your nor the "word 1 to -1" solution will take
> into
> account hard spaces (non-breaking spaces) that could be part of the
> whitespace on either side of the string. So to fix that, you would
> need to
> use:
>
> get matchText(pWhat, "(?s)^\s+(.*?)\s+$", tReturnVal)
>
> I agree that the "word 1 to -1" solution will do what you want 95+% of
> the
> time, but just wanted to make sure that everyone knew there was at
> least one
> 'hole' in that approach in case anyone cared. ;-)
>
> Ken Ray
> Sons of Thunder Software
> Web site: http://www.sonsothunder.com/
> Email: kray at sonsothunder.com
Wow, lots of things happen when sleeping :-).
The matchtext version is soooo much slower (about 30x which makes it
for me a no no in an extended repeat loop).
The hard space if any, can be dealt with in a one time replacement by a
soft space in the container to parse, out of the repeat loop.
(is the non breaking space numtochar(16)? On the mac this is mostly
represented by a non-space character for most fonts (rectangle or down
arrow, etc) in rev/mc)
Once it is replaced the following is faster than the regex to strip
only the "leading" spaces.
Much faster when used inline in a repeat loop (> 10x faster than the
regex replacetext version), less when administered by a function
(around 4x faster):
put word 1 to -1 of pString into x
replace x with "," in pString
put x & item 2 of pString into strippedString (or return x & item 2 of
pString for the function )
You could put this into a oneliner but this makes it about 2x slower
than the above:
return word 1 to -1 of pString & item 2 of (replacetext(pString,word 1
to -1 of pString,","))
but still at least more than 2x faster than the regex replacetext
version.
Regards,
Wouter
More information about the use-livecode
mailing list