RegEx Replace Text -- strip leading and trailing spaces

Ken Ray kray at sonsothunder.com
Wed Apr 13 19:49:28 EDT 2005


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







More information about the use-livecode mailing list