Regex Conjunctions
Most regular expression engines make it easy to match alternations (or disjunctions) with the | operator: to match either foo or bar, use foo|bar.
Few regex engines have any provisions for conjunctions, and the syntax is often horrible. Awk makes it easy to match /pat1/ && /pat2/ && /pat3/.
$ cat <<EOF | awk '/bar/ && /foo/'
> foo bar
> bar
> barfy food
> barfly
> EOF
foo bar
barfy food
In the case of a Unix pipeline, the conjunction could also be expressed as a series of pipes: ... | grep pat1 | grep pat2 | grep pat3 | ....
The longest regex that I ever encountered was an enormous alternation—a true horror that shouldn’t have been a regex at all.
blog comments powered by Disqus