Matching an IP address is another good example of a trade-off between regex complexity and exactness. \b\d{1,3}\.\d{1,3}
To restrict all 4 numbers in the IP address to 0..255, you can use the following regex. It stores each of the 4 numbers of the IP address into a capturing group. You can use these groups to further process the IP number. Free-spacing mode allows this to fit the width of the page.
\b(25[0-5]|2[0-4]
The above regex allows one leading zero for numbers 10 to 99 and up to two leading zeros for numbers 0 to 9. Strictly speaking, IP addresses with leading zeros imply octal notation. So you may want to disallow leading zeros. This requires a slightly longer regex:
\b(25[0-5]|2[0-4]
If you don’t need access to the individual numbers, you can shorten above 3 regexes with a quantifier to:
\b(?:\d{1,3}\.){3}\d{1,3}\b
\b(?:(?:25[0-5]|2
\b(?:(?:25[0-5]|2
The above regexes use word boundaries to make sure the first and last number in the IP address aren’t part of a longer sequence of alphanumeric characters. These regexes are appropriate for finding IP addresses in longer strings.
If you want to validate user input by making sure a string consists of nothing but an IP address then you need to replace the word boundaries with start-of-string and end-of-string anchors. You can use the dedicated anchors \A and \z if your regex flavor supports them:
\A(?:(?:25[0-5]|2
If not, you’ll have to use ^ and $ and make sure that the option for them to match at line breaks is off:
^(?:(?:25[0-5]|2[
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| Regular Expressions Examples | Numeric Ranges | Floating Point Numbers | Email Addresses | IP Addresses | Valid Dates | Numeric Dates to Text | Credit Card Numbers | Matching Complete Lines | Deleting Duplicate Lines | Programming | Homographs | Two Near Words |
| Catastrophic Backtracking | Too Many Repetitions | Denial of Service | Making Everything Optional | Repeated Capturing Group | Mixing Unicode & 8-bit |
Page URL: https://www.regular-expressions.info/ip.html
Page last updated: 13 October 2025
Site last updated: 29 October 2025
Copyright © 2003-2025 Jan Goyvaerts. All rights reserved.