Python (re)

IPv4 Address in PY

Match valid IPv4 addresses with each octet constrained to 0–255.

Try it in the PY tester →

Pattern

regexPY
(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)")
input_text = "192.168.1.1"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Each octet alternation covers 250-255, 200-249, and 0-199 ranges to ensure strict 0-255 validity. Three octets with dots are matched, then the final octet.

Examples

Input

192.168.1.1

Matches

  • 192.168.1.1

Input

255.255.255.0

Matches

  • 255.255.255.0

Input

999.999.999.999

No match

Same pattern, other engines

← Back to IPv4 Address overview (all engines)