Post

Regular Expressions in Action

Matching IPv4 Addresses

# Simple regex to check for an IP address ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$ # Accurate regex to check for an IP address ^(?:(?: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]?)$ # Simple regex to extract IP addresses from longer text \b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b # Accurate regex to extract IP addresses from longer text \b(?:(?: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]?)\b # Simple regex that captures the four parts of the IP address ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$ # Accurate regex that captures the four parts of the IP address ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

refer:

This post is licensed under CC BY 4.0 by the author.
Share