Selecting text using REGEX

J. Landman Gay jacque at hyperactivesw.com
Sun Sep 28 18:17:00 EDT 2003


On 9/28/03 1:39 PM, Alex Rice wrote:

> Here are two trim whitespace functions that had written. To my surprise 
> the regex variant is way, way faster!
> 
> -- 
> -- regex method
> -- 
> function trim pText
>   get replaceText(pText, "^\s+", empty)
>   return replaceText(it, "\s+$", empty)
> end trim
> 
> -- 
> -- non regex method --
> local lWhitespaceChars
> 
> on startup
>  -- prepare list of characters to be used by trim()
>  -- CRLF = ascii 13+10
>  put tab & space & CRLF into lWhitespaceChars
> end startup
> 
> function trim pText
>   repeat while char 1 of pText is in lWhitespaceChars
>     delete char 1 of pText
>   end repeat
>   repeat while char -1 of pText is in lWhitespaceChars
>     delete char -1 of pText
>   end repeat
>   return pText
> end trim
> 

For an easy "trim" function that is just as fast as regex, try this:

   return word 1 to -1 of pText

In a test using text with 1000 leading white space characters and 500 
trailing characters, the difference between the regex method and the 
above one-liner was one millisecond.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com




More information about the use-livecode mailing list