IP Address Regex Go Validator

Search...

⌘K

IP Address Regex Go Validator

Search...

⌘K


IP Address Regex Go Validator

IP Address Regex Go Validator

Validate IPv4 and IPv6 addresses accurately using the IP Address Regex Go Validator. Perfect for cleaning logs, validating user data, or building secure systems. Try this with our Go Regex Tester, or pair it with URL Validator, Email Validator, and GUID Validator for full input validation.

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

Introduction: What Is an IP Address Regex?

In Go, regular expressions (regex) offer a powerful way to validate patterns like IP addresses. IPs are fundamental in any network-based system—whether you’re storing user info, configuring firewalls, or building web applications.

IP addresses come in two types:

  • IPv4 (e.g., 192.168.1.1)

  • IPv6 (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334)


IP Address Regex Patterns

IPv4 Regex Pattern

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


Explanation:

  • Validates 4 octets separated by dots.

  • Each octet ranges from 0 to 255.

  • Example match: 192.168.0.1


IPv6 Regex Pattern

^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$


Explanation:

  • Matches 8 groups of hexadecimal numbers separated by colons.

  • Each group has 1–4 hex digits.

  • Example match: 2001:0db8:85a3:0000:0000:8a2e:0370:7334


How to Validate IPs in Go Using Regex

Use Go’s built-in regexp package to compile the pattern and test strings:


package main

import (
    "fmt"
    "regexp"
)

func isValidIPv4(ip string) bool {
    ipv4Pattern := `^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$`
    regex := regexp.MustCompile(ipv4Pattern)
    return regex.MatchString(ip)
}

func isValidIPv6(ip string) bool {
    ipv6Pattern := `^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$`
    regex := regexp.MustCompile(ipv6Pattern)
    return regex.MatchString(ip)
}

func main() {
    testIPs := []string{
        "192.168.1.1",                    // valid IPv4
        "256.300.88.1",                   // invalid IPv4
        "2001:0db8:85a3:0000:0000:8a2e:0370:7334", // valid IPv6
        "2001:db8::1",                    // invalid (compressed, unsupported here)
    }

    for _, ip := range testIPs {
        fmt.Printf("IP: %s | IPv4: %t | IPv6: %t\n", ip, isValidIPv4(ip), isValidIPv6(ip))
    }
}


Use Cases of IP Address Regex Validation

  • User IP Tracking: Validate IPs before storing them in logs or analytics tools.

  • Access Control: Only allow requests from whitelisted IP ranges.

  • Network Configuration: Automate validation of IPs during setup or provisioning.

  • Data Cleanup: Standardize datasets with malformed or corrupted IP records.


Pro Tips

  • Use IPv6 validation only when required, as many systems still rely primarily on IPv4.

  • Regex doesn’t validate reserved or local IPs (like 127.0.0.1 or ::1)—only format.

  • When working with compressed IPv6 formats (like ::1), consider using the net package for full parsing.

  • Use Go Regex Tester to fine-tune your IP patterns and test edge cases.


Combine with These Tools

Use the IP Address Regex Go Validator in workflows that also require:

Frequently asked questions

Can this validator distinguish between IPv4 and IPv6?×
Yes, it uses separate regex patterns for each format and matches them independently.
Does this handle CIDR notations like 192.168.1.0/24?+
Is the tool case-sensitive for IPv6?+
Does this support compressed IPv6 (e.g., ::1)?+
Can it detect reserved or private IP ranges?+