Ankur Agarwal
Ankur Agarwal

Reputation: 24768

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 abcdef '\(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?

Upvotes: 3

Views: 1896

Answers (2)

fyr
fyr

Reputation: 20869

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.

Upvotes: 2

Priyank Bhatnagar
Priyank Bhatnagar

Reputation: 814

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.

Upvotes: 7

Related Questions