regex/matchtext question

Jim Ault JimAultWins at yahoo.com
Thu Feb 5 06:25:02 CST 2009


Difficulty one is that '[' is a special character in regular expressions and
escaping can make using them even more confusing.  This is also true of the
filter command.

--this function seems to work
--the catch is that the "[" and "]" are special chars
--recall the merge("") function

function getList dertext, derdelimiter, derdelimiter2
  put "<<,>>,{,},[,],{{,}},[[,]]" into oppositeDelm
  if not(derdelimiter is among the items of oppositeDelm)
  then     put "::" into derdelimiter
  if not(derdelimiter2 is among the items of oppositeDelm)
  then    put "::"  into derdelimiter2
  replace cr with numtochar(3) in dertext  --all on one line now
  
  if ( derdelimiter <> derdelimiter2 ) is true then
    replace derdelimiter with (cr & derdelimiter) in dertext
    replace derdelimiter2 with ( derdelimiter2 & cr ) in dertext
    if "]" is in dertext then -- we are using brackets
      replace ( derdelimiter2 & cr ) with ("^^" & cr) in dertext
      filter dertext with ( "*^^" )
      replace "^^" with ( derdelimiter2 ) in dertext  --we are done
    else
      filter dertext with ( "*" & derdelimiter 2 )    --we are done
    end if
  else   --using "::" symetrical double-delimiters
    replace ":" with (":" & cr & ":") in dertext
    replace "::" with empty in dertext
    filter dertext with (":*:")
    replace ":" with "::" in dertext    --we are done
  end if
  return dertext
end getList

Hope this helps.

Jim Ault
Las Vegas

On 2/5/09 2:50 AM, "Klaus Major" <klaus at major-k.de> wrote:

> Hi all,
> 
> I have a nice matchtext script that extract "placeholders" from a text.
> Like everything between "<<" and ">>" or "::" or "{" and "}"
> 
> This is the function I use, it#s a little script of Ken Ray (thanks
> Ken!)
> that I could understand, at least a bit to modify it:-)
> 
> ## extract placeholder tText,tdelimiter,delimiter2
> ## tdelimiter = the delimiter of the returned list of placeholders
> ## derdelimiter2 = the actual "borders" of the placeholders to extract
> function platzhalterextrahieren dertext,derdelimiter,derdelimiter2
> 
>    if derdelimiter = empty then
>      put TAB into derdelimiter
>    end if
>    if derdelimiter2 = empty then
>      put "::" into derdelimiter2
>    end if
> 
>    switch derdelimiter2
>    case "::"
>      put "::" into derdelimiter3
>      break
>    case "<<"
>      put ">>" into derdelimiter3
>      break
>    case "{"
>      put "}" into derdelimiter3
>      break
>    end switch
> 
>    repeat
>      get matchText(dertext,"(?s)" & derdelimiter2 & "(.*?)" &
> derdelimiter3,wert)
>      if it = false then
>        exit repeat
>      end if
>      put derdelimiter2 & wert & derdelimiter3 into neu
>      put neu & derdelimiter after neueliste
>      replace neu with empty in dertext
>    end repeat
> 
>    delete char -1 of neueliste
>    return neueliste
> end platzhalterextrahieren
> 
> This results in a list like:
> ::Anrede_90001::
> ::Titel_90001::
> ::Nachname_90001::
> 
> Or:
> <<Anrede_90001>>
> <<Titel_90001>>
> 
> Or:
> {Anrede_90001}
> {Titel_90001}
> 
> You get the picture.
> 
> Works fine so far, but now I also wanted to use "[" and "]" and even
> "[[" and "]]"
> but I cannot get the script to work. I tried to escape these
> characters like:
> ...
> case "["
>      put "\[" into derdelimiter2
>      put "\]" into derdelimiter3
>      break
> ...
> 
> I need to replace the german decimal delimiter COMMA with a dot before
> I can
> finally MERGE the content of the text, and replace it with a COMMA
> after the MERGE.
> Know what I mean?
> 
> But then I get stuck in an endless repeat loop?!
> 
> Is this possible at all with matchtext?
> Any hints are highly appreciated!
> 
> Thanks a lot in advance!


this is the sample for the test ::variable1:: and it should extract the
::real strings like this:: and jettison the rest with only ::keepers:: left
in a listing





More information about the use-livecode mailing list