IP Address Regex Python Validator

Search...

⌘K

IP Address Regex Python Validator

Search...

⌘K


IP Address Regex Python Validator

IP Address Regex Python Validator

The IP Address Regex Python Validator helps you ensure that any string input follows the correct format for an IPv4 address. It’s perfect for use in server configurations, firewall filters, form validations, and networking tools. You can combine it with the Mac Address Regex Python Validator for device-level filtering, or pair it with the UUID Regex Python Validator when working with both identifiers and network data.

127.0.0.1
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "127.0.0.1" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What Is an IP Address?


An IP address is a unique identifier assigned to each device on a network. IPv4 addresses use four numbers (0–255) separated by dots, like:

192.168.0.1


To validate this format, we use a regex pattern that checks:

  • Exactly four octets

  • Each ranging from 0–255

  • Dot separators (.)


Regex Pattern for IP Address in Python


Here’s a regex that accurately validates standard IPv4 addresses:

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


Explanation:

  • 25[0-5] → matches 250–255

  • 2[0-4][0-9] → matches 200–249

  • 1?[0-9]{1,2} → matches 0–199

  • The entire pattern ensures 4 such numbers, separated by .


Python Code Example


import re

def is_valid_ip(ip):
    pattern = re.compile(
        r'^((25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})\.){3}'
        r'(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})$'
    )
    return bool(pattern.fullmatch(ip))

# Test inputs
ips = [
    "192.168.0.1",
    "255.255.255.255",
    "999.100.100.100",  # Invalid
    "172.16.254.01"
]

for ip in ips:
    print(f"{ip} -> {is_valid_ip(ip)}")


Use Cases



Pro Tips


  • 🔄 Always trim whitespaces before validation (ip.strip())

  • 🔒 Avoid trusting client-side validations—validate on the server too

  • 🔀 Combine with subnet pattern checks for CIDR ranges

  • 🧠 Use in scripts with the GUID Regex Python Validator to track devices over networks


Frequently asked questions

Does this regex validate IPv6?×
No. This pattern only matches IPv4 addresses. Use a different pattern for IPv6.
Can it validate IPs in CIDR notation like 192.168.1.0/24?+
Are leading zeros in octets valid?+
Can this be used in Flask or Django?+
What happens if input has extra dots or characters?+