Networking

IP Address Regex: IPv4 Structure and Correct Validation

A first IPv4 Regex usually checks for four numeric groups separated by dots. The harder part is limiting each group to the range 0 through 255.

Before you start: test every pattern with examples that should match and examples that should fail.

Basic IPv4 structure

This pattern verifies four groups of one to three digits. It is useful for teaching and extracting candidates, but it accepts out-of-range values.

Regex: ^(?:\d{1,3}\.){3}\d{1,3}$

The range problem

999.999.999.999 has the right shape but is not a valid IPv4 address. Encoding 0-255 directly in Regex is possible, but much less readable.

When to use a parser or library

For production validation, networking utilities from your language are often clearer and more reliable, especially once IPv6 is involved.

Match the solution to the task

Extracting IP-looking values from logs may only need the structural pattern. Validating a network configuration needs semantic validation.

Next step: test it for real

Use the Playground to experiment with the pattern, then practice in the game with feedback on false positives and false negatives.

Practice in the gameTest in Playground

Keep learning

← Back to learning hub