0

I'm currently learning regex will this detec any IP address?

(((([0-9]|[1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]|25[0-5])){1,3})(\.){0,1}){4}

My previous one was this

(([0-9]|[1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]|25[0-5]))\.(([0-9]|[1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]|25[0-5]))\.(([0-9]|[1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]|25[0-5]))\.(([0-9]|[1-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9]|25[0-5]))
  • why not use an online regex tester? regex101.com – George Udosen Oct 21 '19 at 18:28
  • 2
    This doesn't seem particularly isolated to Ubuntu, probably better places in the Stack Exchange network to source this answer. Try: stackoverflow.com/q/5284147/2943403 – mickmackusa Apr 9 at 11:37
  • Regex may be not the best for this, instead .. comparing 4 bytes digit – user.dz Apr 21 at 13:40
1
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d\.){3}25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d

This pattern does not include word boundaries or start/end of string anchors, so depending on the context of usage, you may need to wrap the pattern in these.

Pattern breakdown:

(?:         #start non-capturing group
  25[0-5]   #match 250 through 255
  |         #or
  2[0-4]\d  #match 200 through 249
  |         #or
  1\d\d     #match 100 through 199
  |         #or
  [1-9]?\d  #match 0 through 99
  \.        #match a literal dot
){3}        #end of non-capturing group and require three repetitions of the same pattern
25[0-5]   #match 250 through 255
|         #or
2[0-4]\d  #match 200 through 249
|         #or
1\d\d     #match 100 through 199
|         #or
[1-9]?\d  #match 0 through 99
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.