-1

Please advice what wrong in my syntax , and why the following regular expression doesn’t work

this example was tested under bash shell:

echo 12.212.12.198 | grep "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]| [1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
  • Not get any output

The same about all IP’s as 192.9.200.12 , 172.18.12.34 , etc …

maihabunash
  • 1,632
  • 9
  • 34
  • 60

1 Answers1

1

You are missing -E (PATTERN is an extended regular expression (ERE))
Here is one good working found on google:

grep -E "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$"

This as four part of ([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) that work like this

[1-9]           # 1-9
[1-9][0-9]      # 10-99
1[0-9][0-9]     # 100-199
2[0-4][0-9]     # 200-249
25[0-5]         # 250-255

Leading 0 is not valid, so this does not give valid if there is leading 0

Jotne
  • 40,548
  • 12
  • 51
  • 55