Why does expr match outputs different things when the pattern (not) contains a parenthesized group?
Why does
$ echo `expr match abcdef 'abc'`
give number of characters matched, which is 3, but
$ echo `expr match a开发者_运维百科bcdef '\(abc\)'`
gives the characters matched , which is abc ?
I understand regex matching is in play here, but cannot understand how having a parenthesized sub expression is making this difference here?
This is from the man page of expr :
Pattern matches return the string matched between \( and \) or null; if \( and \) are not used, they return the number of characters matched or 0.
Man page.
That has nothing to do with regular expressions. It is only a difference how the command "expr" works. The first one returns the length of the matching substring and the second one returns the matching substring itself.
There is a very good summary: TLDP refcard. You find a summary of combinations how expr can be used.
精彩评论