Matchtext for multiple words

John Craig jc at spl21.net
Wed Nov 29 19:51:44 EST 2006


And a script to create the regex from a word list.  My apologies if this 
stuff turns out useless - but you can get absorbed in this mince...


on mouseUp
  -- string to search * SHOULD MATCH
  put "The purple dinosaur inadvertently stepped on the cat." & return & 
"The white dog howled." into tString
 
  -- DUFF string to search ** SHOULD NOT MATCH
  put "The purple dinosaur inadvertently stepped on the cat." & return & 
"The white dinosaur howled." into tString2
 
  -- words to find
  put "cat,dinosaur,dog" into tWords
 
  -- build the pattern to match the words
  put "(" into tWordsPattern
  repeat for each item tWord in tWords
    put tWord & "|" after tWordsPattern
  end repeat
  put ")" into char -1 of tWordsPattern
 
  -- build the whole damn regex
  put num of items in tWords into tTotalWords
  put 0 into tCurrentWord
  put "(?is)" into tRegex
  repeat for each item tWord in tWords
    add 1 to tCurrentWord
    put "\b" after tRegex
    if tCurrentWord > 1 then
      put "(?!" after tRegex
      repeat with i = 1 to tCurrentWord - 1
        put "\" & i & "|" after tRegex
      end repeat
      delete char -1 of tRegex
      put ")" after tRegex
    end if
    put tWordsPattern & "\b" after tRegex
    if tCurrentWord < tTotalWords then
      put ".*" after tRegex
    end if
  end repeat
 
  -- test our regex against the 2 test strings
  put matchText(tString, tRegex) & return & matchText(tString2, tRegex)
 
end mouseUp


John Craig wrote:
> I still think it's working ok - someone slap me if I'm wrong.
> The (?!  is looking ahead and saying 'you can't begin with.
>
> (?!\1) - you can't begin with the first match
> (?!\1|\2) - you can't begin with the 1st or second match
>
> JC
>
> J. Landman Gay wrote:
>> Sorry if this comes through twice, I'm having trouble sending to the 
>> list.
>>
>> I need a matchtext/regex that will tell me if all supplied words 
>> exist in a block of text, regardless of their order, and ignoring 
>> carriage returns.
>>
>> For example, see if all these words:  dog dinosaur cat
>>
>> exist in this text:
>>
>> "The purple dinosaur inadvertently stepped on the cat.<cr>
>> The white dog howled."
>>
>> Should return true. Is there such a thing?
>>
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your 
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>




More information about the use-livecode mailing list