RegexRef
🖥️

Regex for IPv4 Address

Matches a valid IPv4 address (0-255 blocks).

The Pattern

/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Test in Visualizer ↗

✅ Matches

  • 192.168.1.1
  • 255.255.255.0

❌ Non-Matches

  • 256.1.1.1
  • 192.168.1
  • 192.168.1.1.1

Implement in your Code

javascript
const regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
regex.test('192.168.1.1');
python
import re
regex = re.compile(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
bool(regex.match('192.168.1.1'))
go
import "regexp"
matched, _ := regexp.MatchString(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`, "192.168.1.1")
rust
use regex::Regex;
let re = Regex::new(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$").unwrap();
re.is_match("192.168.1.1")