RegEx Replace Text -- strip leading and trailing spaces

Trevor DeVore lists at mangomultimedia.com
Wed Apr 13 15:47:48 EDT 2005


On Apr 13, 2005, at 12:25 PM, Wouter wrote:
> On 13 Apr 2005, at 18:09, Trevor DeVore wrote:

>> I use this regex to strip whitespace:
>>
>> get replaceText(pString, "^[ \t\r\n]+|[ \t\r\n]+$", "")
>>
> No offence, but this won't strip whitespaces at both ends of a string 
> in one pass.
> And it is about ten times slower for one pass than: get word 1 to -1 
> of pString
> (which does strip leading and ending whitespaces in one pass, but true 
> it is no regex).

None taken.  Your're right.  I put that together using my ltrim and 
rtrim functions and didn't test it.  I think the correct regex syntax 
should be something like this:

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 ;)

Is there a way to do ltrim (trim whitespace on left of string) in plain 
transcript?  Right now I just use:

function str_lTrim pString
	get replaceText(pString, "^[ \t\r\n]+", "")
	return it
end str_lTrim


-- 
Trevor DeVore
Blue Mango Multimedia
trevor at mangomultimedia.com



More information about the use-livecode mailing list