Balaji
Balaji

Reputation: 123

regular expression in grep

I'm trying to match a valid Ip address using a regular expression. I have an expression bu the problem is I'm not able to understand grep behavior.

to match numbers in the range 0-255 I did : echo 15 | grep -E "[01]?[0-9][0-9]?|25[0-5]|2[0-4][0-9]"

But it matches all the numbers!

for example 3000 is also matched- I think its because it match "30" which is part of whole string 3000... then I tried grep -E "([01]?[0-9][0-9]?|25[0-5]|2[0-4][0-9])$"

(trying to match 0-255 followed by end of line).. But that doesn't seem to be working. any suggestions??

Is it valid to to put parenthesis to separate parts of regular expression? some thing like this -> ((a|b)c)(l|m)

Upvotes: 0

Views: 382

Answers (1)

Paul Tomblin
Paul Tomblin

Reputation: 182772

 echo 15 | grep -E "^([01]?[0-9][0-9]?|25[0-5]|2[0-4][0-9])$"

Upvotes: 1

Related Questions