Regular expression escape characters

Peter M. Brigham, MD pmbrig at gmail.com
Thu Mar 29 09:39:08 EDT 2012


On Mar 28, 2012, at 11:19 PM, Bill Vlahos wrote:

> I am building a regular expression to find a set of question marks and exclamation points. For example ??? or !!!.
> 
> These are special characters so they mean something to a regular expression.
> 
> How do I find them in the text?
> 
> If I want to find a word such as "Bill" I can matchText with (\bBill\b) but I can't do (\b!!!\b) and if I do (\b???\b) I get an error.
> 
> I've looked on the web for escaping characters and they suggest using a slash before. However, when I try (\b\!\!\!\b) it doesn't work.
> 
> I've also found references to \Q…\E to have everything between a literal but (\b\Q!!!\E\b) doesn't work either.
> 
> Any ideas?

I have a bias (often warranted) towards using LC's text and chunking capabilities rather than LC. If you know that the runs of question marks and exclamation points will alway be length = 3 then you could avoid Regex:

put offsets("???",tText) into qOffsets
put offsets ("!!!",tText) into eOffsets
-- do something with these lists

function offsets str,cntr
   -- returns a comma-delimited list of all the offsets of str in cntr
   if str is not in cntr then return 0
   put "" into offsetList
   put 0 into startPoint
   repeat
      put offset(str,cntr,startPoint) into thisOffset
      if thisOffset = 0 then exit repeat
      add thisOffset to startPoint
      put startPoint & comma after offsetList
   end repeat
   delete last char of offsetList
   return offsetList
end offsets

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the use-livecode mailing list