James Raitsev
James Raitsev

Reputation: 96391

Clarifying regular expression

Assuming sentence

"The time is now 8:15pm. Followed by some other text"

Why would the following regex not match the line?

egrep '\<[1-9]\s*:\s*[0-9]{2}\s*[Pp][Mm]\>' file

I am on MAC, using GNU egrep

$ egrep --version egrep (GNU grep) 2.5.1

Upvotes: 0

Views: 84

Answers (3)

Mark Reed
Mark Reed

Reputation: 95252

Totally unable to reproduce.

Mac OS X 10.7:

(0)marks-mac-mini> cat file
The time is now 8:15pm. Followed by some other text
"The time is now 8:15pm. Followed by some other text"
(0)marks-mac-mini> egrep '\<[1-9]\s*:\s*[0-9]{2}\s*[Pp][Mm]\>' file
The time is now 8:15pm. Followed by some other text
"The time is now 8:15pm. Followed by some other text"
(0)marks-mac-mini> egrep --version
egrep (GNU grep) 2.5.1

So you're wrong about something somewhere...

EDIT Ah, the problem is the space. egrep doesn't recognize \s; that's a Perlism. Your question didn't include the space there.

Upvotes: 2

plasma
plasma

Reputation: 898

Using GNU egrep on my OS X machine, the expression works just fine and matches your line. It could be your particular version of grep (is it a non-GNU grep?)

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102753

It's because of the leading and trailing symbols in the regex: < and >. Take those out and it's a match.

Upvotes: 2

Related Questions