0

Regex 1: \b(([01]?\d\d?|2[0-4]\d|25[0-5])(\.|\b)){4}\b

Regex 2: \b(([01]?\d\d?|2[0-4]\d|25[0-5])(\.|$)){4}\b

Sample String :

123.105.114.142
010.000.000.011
240.000.000.240 121.122.221.123 224.152.255.152 
240.345.334.123

..@102.021.031.246@@@@@@02.021.031.246@@@@..@
255.255.251.255
10.1.1.2
23.124.23.144#

Regex 1 is able to fetch all the ip addresses from the sample string. My question is why regex 2 is not able to do so?

Madhur
  • 425
  • 1
  • 3
  • 10
  • https://regex101.com/ – mplungjan Apr 23 '18 at 07:57
  • I tried that regex on regex101.com {4} means 4 times – Madhur Apr 23 '18 at 08:04
  • 1
    Yes, I know, but WHAT 4 times. The 101 shows nothing when you have it in. The first one does have the preceding element 4 times - there are no 4 iterations of `([01]?\d\d?|2[0-4]\d|25[0-5])(\.|$)` because of the $ – mplungjan Apr 23 '18 at 08:07
  • `$` means the end of the string. If you remove `\b` and add `.` after all IPs in the input, your second one will also extract those IPs. – Wiktor Stribiżew Apr 23 '18 at 08:16
  • @WiktorStribiżew I realized my mistake. My second regex will work only when my input is a string ending in xxx.xxx.xxx.xxx where x is a digit. – Madhur Apr 23 '18 at 08:57
  • Probably. Just read a bit more on regexes. See http://regexone.com - it is useful to learn the basics (as anchors and word boundaries). Use http://regex101.com to debug regex patterns (with PCRE option, it allows you to peek at what is going on behind the scenes). – Wiktor Stribiżew Apr 23 '18 at 08:58
  • @WiktorStribiżew Thanks a lot for that pcre debug hack. – Madhur Apr 23 '18 at 09:08

0 Answers0