Updated 2006-08-25 15:09:34 by suchenwi

Assuming all words are simple REs (no metacharacters):
 proc findAllWords {wordList text} {
    set start 0
    while {[llength $wordList]} {
       set RE [join $wordList |]
       if {![regexp -start $start -indices $RE $text range]} {
          return 0
       set word [eval [list string range $text] $range]
       set idx [lsearch -exact $wordList $word]
       set wordList [lreplace $wordList $idx $idx]
       set start [expr {[lindex $range 1]+1}]
    return 1

RS understood the task differently, more like cascaded greps:
 proc findAllWords {wordlist linelist} {
    set res {}
    foreach line $linelist {
        set missed 0
        foreach re $wordlist {
            if {![regexp -- $re $line]} {set missed 1; break}
        if {!$missed} {lappend res $line}
    return $res

WJP Posting code for a problem that isn't stated isn't all that helpful. Could someone please state the problem? - RS: The problem came uo in [the Tcl chatroom]: a substitute for agrep in pure Tcl which allows searching for several terms in no particular order. E.g. agrep 'foo;bar;grill' will report lines that contain "foo..grill..bar" or "bar..grill..foo", etc.