3

Could anyone help me to understand how the regex match works with the expr utility? I've read its man page and below is an excerpt:

STRING : REGEXP
  anchored pattern match of REGEXP in STRING

But I can't understand how it works. I did some testing:

[root@192 ~]# expr "abc" : '.*'
3
[root@192 ~]# expr "abc" : 'b.*'
0
[root@192 ~]#

What is expr doing in these two commands? For the first command, it seems expr found a match from the first character of abc and reports the matched length. But why does it produce 0 for the second command? I just don't understand the logic here.

Btw, I know how regexes work.

terdon
  • 234,489
  • 66
  • 447
  • 667
Fajela Tajkiya
  • 965
  • 5
  • 15

1 Answers1

5

The relevant part of the quoted manpage is that these are anchored matches. This means that the regex will need to match the entire string, from the beginning to the end. So your .* is actually ^.*$. The man page is admittedly not very clear, but yes, it looks like it will print the length of the match by default:

$ expr "abc" : '.*'
3

You can apparently get it to print the match itself using capture groups:

$ expr "abc" : '\(.*\)'
abc
$ expr "abc" : '.\(b.\)'
bc
terdon
  • 234,489
  • 66
  • 447
  • 667