- Home
- Tools
- Developer Tools
- Regex Tester
- IP Address Regex
IP Address Regex
A regular expression for IPv4 addresses that checks every number is between 0 and 255, filled in with test lines.
JavaScript syntax, run by your browser's own RegExp engine.
5 matches.
Matches
- 1
192.168.1.1index 0 - 2
10.0.0.254index 12 - 3
8.8.8.8index 23 - 4
0.0.0.0index 31 - 5
255.255.255.255index 39
Cheat sheet
Characters
- .
- Any character except a line break
- \d \D
- A digit 0-9, anything but a digit
- \w \W
- A letter a-z, a digit or an underscore; anything else
- \s \S
- White space, anything but white space
- [abc] [^abc]
- One of a, b, c; any character except those
- [a-z0-9]
- A character in one of the ranges
- \p{L}
- A letter in any script (needs the u flag)
- \. \\ \/
- A literal dot, backslash, slash
How many
- a*
- Zero or more
- a+
- One or more
- a?
- Zero or one
- a{3}
- Exactly three
- a{2,5}
- Two to five
- a{2,}
- Two or more
- a*? a+? a??
- The same, but as few as possible (lazy)
Positions
- ^
- Start of the text, or of a line with the m flag
- $
- End of the text, or of a line with the m flag
- \b
- The edge of a word
- \B
- Anywhere that is not the edge of a word
Groups
- (abc)
- A group whose text is remembered
- (?<name>abc)
- The same, with a name
- (?:abc)
- A group that is not remembered
- a|b
- Either a or b
- \1 \k<name>
- The same text a group matched earlier
Looking around
- a(?=b)
- a, only when b follows
- a(?!b)
- a, only when b does not follow
- (?<=b)a
- a, only when b comes before it
- (?<!b)a
- a, only when b does not come before it
In the replacement
- $1 $2
- The text of group 1, group 2
- $<name>
- The text of a named group
- $&
- The whole match
- $` $'
- The text before the match, after the match
- $$
- A dollar sign
The pattern, piece by piece
For IPv4 addresses in the usual notation: four numbers from 0 to 255 with dots between them.
/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/gm| Piece | What it does |
|---|---|
^ | The start of the line. |
(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3} | Three times: a number from 0 to 255 and a dot. |
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d) | The fourth number, without a dot behind it. |
$ | The end of the line. |
How a pattern counts to 255
A regular expression compares characters, not values. The range 0 to 255 therefore has to be spelled out as four shapes:
| Piece | What it does |
|---|---|
25[0-5] | 250 to 255. |
2[0-4]\d | 200 to 249. |
1\d\d | 100 to 199. |
[1-9]?\d | 0 to 99, without a zero in front. |
What it gets right
These are the first lines in the box above. With the g and m flags every line is tested on its own, from its first character to its last.
| Line | Result | Why |
|---|---|---|
192.168.1.1 | Match | |
10.0.0.254 | Match | |
8.8.8.8 | Match | |
0.0.0.0 | Match | |
255.255.255.255 | Match | |
256.1.1.1 | No match | 256 is one too many. |
192.168.1 | No match | Three numbers. |
192.168.1.1.1 | No match | Five numbers. |
192.168.01.1 | No match | A zero in front. Refused on purpose: some programs read 01 as an octal number. |
1.2.3.4/24 | No match | A network with its prefix length, not one address. |
Where it goes wrong
The last lines in the box are there to show the limits: text the pattern refuses although it is fine, or lets through although it is not.
| Line | Result | What is wrong with that |
|---|---|---|
2001:db8::1 | No match | An IP address too, of the newer kind: IPv6. |
::1 | No match | The IPv6 address of the machine itself. |
Before you rely on it
The pattern knows the notation and not the meaning. It does not tell a private address such as 192.168.1.1 from a public one.
IPv6 has so many shorthand forms that a pattern for it runs to several lines. In code, ask the platform instead: net.isIP() in Node.js, ipaddress.ip_address() in Python.
To find addresses in a log line instead of testing whole lines, put \b where the ^ and the $ are.
The regex tester page explains the flags, has a cheat sheet of the syntax, and tells what happens when a pattern takes too long.