This is an old revision of the document!
Overview
I think the Linux expr
command is woefully underused for matching and extraction of substrings using basic regular expressions, so herewith, a quick primer.
Consider a fully-qualified package name with version number and, say, patch level number:
$ P="fubar-1.2.3-42"
Say one wanted to match or extract different components of that string – this variation of the expr
command will, first, simply count the number of matching characters based on an anchored match; that is, anchored to the beginning of the string.
Let's count the length of the substring that matches anything but that first hyphen:
$ expr $P : '[^-]*' 5 $