1

When using Python 3 with Regex I am getting unexpected results. I want to do something like this

firstpass = re.findall('\s+((10|192|172).\d{1,3}.\d{1,3}.\d{1,3}/(\d+))', line)

Where it would look for spaces, then numbers starting with 10 or 192 or 172 and then any 3 octets after (IPv4 addresses).

I understand the grouping but I either get only one of the first three values. Or if I change the grouping around I get get 3 returns (but still only 1 of the initial search values) I've tried as many variations as I can read about. the [10|192|172] just doesn't work. inside () groupings of course.

I am looking for two return strings. The IP and the mask. It works fine until I try to enlarge my expression to have a more complex search.

the end goal is to look for ANY private IP space in one regex expression.

10.match.match.match
172.16-32.match.match
192.168.match.match

So the data may look like this and I want to pull 1 -3 but not the last line

B    10.50.13.0 /26 blah foo
O    192.169.5.0 /24 foo foo
B    172.18.0.0/16 etc etc
B    82.33.4.0/24 yak yak yak
2
  • 1
    Try regex101.com/r/d57ABN/1 Commented Apr 3, 2018 at 18:38
  • OMG the greatest site ever invented! Thank you sir! I need to get my head around word boundaries but it's what I needed for sure. Commented Apr 3, 2018 at 19:20

1 Answer 1

1

You need to escape all dots (since you want to match them as literal chars), only use capturing groups around those patterns that you want re.findall to return and use non-capturing groups around the patterns you do not need it to yield, and use word boundaries.

re.findall(r'\b((?:10|192|172)(?:\.\d{1,3}){3}\s*/(\d+))', s)

See the regex demo.

See the Python demo:

import re
rx = r"\b((?:10|192|172)(?:\.\d{1,3}){3}\s*/(\d+))"
ss = ["B    10.50.13.0 /26 blah foo", "O    192.169.5.0 /24 foo foo", "B    172.18.0.0/16 etc etc", "B    82.33.4.0/24 yak yak yak"]
for s in ss:
    print(s)
    m = re.findall(rx, s)
    if m:
        print(m)
    else:
        print("NO MATCH")

Output:

B    10.50.13.0 /26 blah foo
[('10.50.13.0 /26', '26')]
O    192.169.5.0 /24 foo foo
[('192.169.5.0 /24', '24')]
B    172.18.0.0/16 etc etc
[('172.18.0.0/16', '16')]
B    82.33.4.0/24 yak yak yak
NO MATCH
Sign up to request clarification or add additional context in comments.

2 Comments

the ?: does't work unless I make it raw it seems. Weird. but yes this is what works. The regex tester is perfect for where I am at with regex. Thank you. Word Boundaries. My new topic of interest :)
@Seth The word boundary should be defined as r'\b' or '\\b'. The '\b' is a backspace char. Non-capturing group syntax is not affected by the type of the string literal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.