Effective regex for repeated subsequences

I am trying to build regexp where it is expected that the subsequence will occur in different scenarios throughout the match. A similar example is the I / P address: for the input string blah129.186.51.101blah match: 129.186.51.101 , but the subsequences 129, 186, 51 and 101 each satisfy the matching condition that they fall into 0 and 255 (simplified condition). So my regex ends up with something like:

 (?:{regexp for 0 to 255}\.){3}({regexp for 0 to 255}) 

Is there a more elegant way to handle such scenarios where a regular expression can have smaller fragments of a regular expression that can again refer to the entire regular expression?

+4
source share
3 answers

Perhaps you can try the following:

 ((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?!\d)\.??){4} 

although this will happen in a special case such as blah129.186.51.1011blah , but I think it should be a coincidence with an error, since it is not a valid ip?

+3
source

This will solve your problem:

 ((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?!\d)\.??){4} 

Anchor (see test results):

 ^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$ 

Test results:

 // Valid IP addresses // 1.2.3.4 // 255.255.255.255 // Invalid IP addresses // 1.2.3 // 1.2.3. // .1.2.3 // 1.2.3.4.5.6.7.8 (use anchors ^ and $ to skip these if needed, since 1.2.3.4 and 5.6.7.8 will still be captured) // 999.999.999.999 // 299.299.299.299 // 001.002.003.004 (these use octal notation, not decimal) 

An anchored regular expression will not match the inner text only if you want a very strict match for a string that should only contain IP

Update:

Real time results here

+2
source

Also, look at this pattern: (May also match leading zeros)

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

Living example

EDIT

I suggested a regex, but I would never recommend doing it with Regex. You should use the SPLIT function and SPLIT over the indices and compare it with a range from 0 to 255. See My comment at the top for this.

As for Regex, you can prefix and suffix this pattern with ^ and $ respectively, if you have an IP address in the string.

+1
source

Source: https://habr.com/ru/post/1485549/


All Articles