Good point, this avoids a line-based loop altogether and again imrproves performance smile

I found an old snippet of mine, modified it and added comments, to illustrate the $replace-stuff required for a regex approach (which will keep lines in order)
Code:
alias examplesearch {
  var %infile = versions.txt, %outfile = search results.txt
  var %reg

  ; prompt for (next) search string
  while ($input(Enter $iif(%reg,next) search string or nothing to $iif(%reg,start,abort) the search:,eog,Search for one or more strings in %infile) != $null) {

    ; transform text string into regex string
    ; 1) $regsubex:   reduce consecutive asterisks (not mandatory, bot won't hurt)
    ; 2) $replacexcs: 
    ;    a) replace wildcard chars "asterisk" and "questionmark" with regex equivalents
    ;    b) replace whitespaces with regex equivalents (else you'll loose single trailing- and consecutive whitespaces)
    ;    c) "quote" all other text (else chars like "[ ] { } ^ | \ . +" wouldn't be treated literally and likely would corrupt the regex)
    ; 3) $removecs:   clean regex by removing empty (redundant) quotings "\Q\E" (not mandatory)
    
    var %s = $removecs( $+(\Q, $replacexcs($regsubex($!,/\*+/g,*), *,\E.*\Q, ?,\E.\Q, $chr(32),\E\x20\Q, \E,\E\\E\Q) ,\E) ,\Q\E)

    ; add string to the regex
    var %reg = $iif(%reg,$v1 $+ |) $+ %s
  }

  ; one or more search terms to process
  if (%reg) {
    ; filter infile to outfile (regex enclosed in /<regex>/ and "i" switch added for a case insensitive search)
    filter -ffcg $qt(%infile) $qt(%outfile) $+(/,%reg,/i)

    ; report result, open outfile (if any matches and not locked)
    echo -agc info Lines of %infile matching your search string(s): $filtered
    if ($filtered) && (!$lock(run)) { run $qt(%outfile) }
  }
}
...maybe it's of use.

Last edited by Horstl; 03/12/09 12:51 PM.