Passing contents of a variable to a regular expression

Ken Ray kray at sonsothunder.com
Tue Sep 17 21:01:01 EDT 2002


Sannyasin,

Thanks for the kudos. Here's what you can do - you can simply make the
regular expression into a variable. Keep in mind that you may actually have
*two* identifiers (and open paren and a close paren in this case), so you'll
either need two preferences, or one preference that could be two characters
long. Also, I noted that your matchText function assumes you have a local
variable tDeletion that was not declared in your posted code, so I added it.

on cleanUpEditedOriginal
  local tDeletion
  put fld "deleteDel" of stack "prefs" into lDeleteDel
  if (length(lDeleteDel) > 2) or (length(lDeleteDel = 0) then
    answer "Bad Delimiter Length"
    exit cleanUpEditedOriginal
  else
    if length(lDeleteDel) = 2 then
      put char 1 of lDeleteDel into tOpenDel
      put char 2 of lDeleteDel into tCloseDel
    else
      put lDeleteDel into tOpenDel
      put lDeleteDel into tCloseDel
    end if
  end if

  put fld "original" into jai
  put "(\" & tOpenDel & ".*?" & "\" & tCloseDel & ")" into regEx
  repeat until tToggle = 1
    if matchtext (jai,regEx,tDeletion) then
      replace tDeletion with "" in jai
    else
      put 1 into tToggle
    end if
  end repeat
  replace "  " with " " in jai
  put jai into fld "clean"
end cleanUpEditedOriginal

Note that not all characters need to be escaped with a "\". So you'd need to
adjust the code to optionally escape the characters that need to be escaped,
and leave alone the ones that don't.

Hope this helps,

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/



----- Original Message -----
From: "Sannyasin Sivakatirswami" <katir at hindu.org>
To: <use-revolution at lists.runrev.com>
Sent: Tuesday, September 17, 2002 4:33 PM
Subject: Passing contents of a variable to a regular expression


> I have a stack where editors are to use a single character to  tag a
> word to be deleted... But I want them to be able to custom select the
> character to serve as this delimiter, store that as a preference and
> then later pull it into a regular expression... but I don't see how to
> do that:
> (Thanks to Ken Ray for getting me this far!)
>
> on cleanUpEditedOriginal
>    put fld "deleteDel" of stack "prefs" into lDeleteDel
>       ## assume its an "*" asterisk for this test
>    put fld "original" into jai
>    repeat until tToggle = 1
>      if matchtext (jai,"(\(.*?\))",tDeletion) then
>         ## in this case "()" are wrapping text to be deleted.
>         ## but we want to use the contents of lDeleteDel  in this
> expression
>         ## as in something like: "(\[lDeleteDel].*?\[lDeleteDel])"
> ## which would resolve to "(\*.*?\*)" in this case
>         ## where lDeleteDel is an * (asterisk--but it could be any
> single char)
>        replace tDeletion with "" in jai
>      else
>        put 1 into tToggle
>      end if
>    end repeat
>    replace "  " with " " in jai
>    put jai into fld "clean"
> end cleanUpEditedOriginal
>
> Any clues?
>
>
> Thanks!
> Himalayan Academy Publications
> Sannyasin Sivakatirswami
> Editor's Assistant/Production Manager
> katir at hindu.org
> www.HinduismToday.com, www.HimalayanAcademy.com,
> www.Gurudeva.org, www.hindu.org
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution
>




More information about the use-livecode mailing list