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 initial substring that matches anything but a hyphen:
$ expr $P : '[^-]*' 5 $
Questionably useful, I guess, so let's extend this by counting the number of characters in the combined package number and version (and intervening hyphen):
$ expr $P : '[^-]*-[^-]*' 11 $
Finally, we could, of course, use that trick to match the entire string based on the pattern we know it will have (allowing a simple “.*” to suck up the rest of the string):
$ expr $P : '[^-]*-[^-]*-.*' 14 $
“So what?”, you think. Ah, but you can also tag the field you care about to have that field value printed, not simply its length.
Extract the package name:
$ expr $P : '\([^-]*\)' fubar $
Extract the version number:
$ expr $P : '[^-]*-\([^-]*\)' 1.2.3 $
Finally, extract the patch level:
$ expr $P : '[^-]*-[^-]*-\(.*\)' 42 $ Piece of cake.