Sooner or later a form, a log file or a config check asks the same question: is this text an IPv4 address? A regular expression looks like the obvious tool, and it is, up to a point. This guide builds the standard IPv4 pattern piece by piece, tests it in the free Regex Tester on Ovatly against strings that pass and fail, and states the catch plainly: the simple pattern checks the shape of an address, not the range of its numbers.

Contents

  • Quick answer
  • What an IPv4 address is made of
  • Building the pattern piece by piece
  • How to test the pattern, step by step
  • Worked example: five test strings
  • The stricter octet pattern
  • Limitations and alternatives
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

The simple IPv4 pattern is ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$: four groups of one to three digits, joined by escaped dots, anchored so the whole string must match. Paste it into the Regex Tester, without slashes and with no flags needed, and it will match 192.168.0.1. It will also match 999.999.999.999, because the pattern checks shape only and says nothing about the 0 to 255 range. If the range matters, use the stricter octet pattern further down, or validate in code.

What an IPv4 address is made of

An IPv4 address is four numbers separated by dots, like 192.168.0.1. Each number is called an octet, and each one runs from 0 to 255. That gives a regex two separate jobs: checking the layout (four numbers, three dots, nothing else) and checking the range (no number above 255). The simple pattern does the first job well. The second is where regex gets awkward, and pretending otherwise is how invalid addresses slip into production data.

Building the pattern piece by piece

Start with one octet. Digits are \d, and an octet has between one and three of them, so one octet is \d{1,3}. The quantifier {1,3} means "repeat the previous item at least once and at most three times".

The dots between octets need care. In a pattern, a plain . means "any character", so an unescaped dot would happily match the letter x in 192x168x0x1. Escaping it as \. makes it mean a literal dot.

Join four octets with three escaped dots and you get \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}. One piece remains: the anchors. Without them, the pattern matches anywhere inside a longer string, so version 10.0.0.256 final would produce a match on a substring. Adding ^ at the start and $ at the end forces the pattern to cover the whole string, which is what validation usually means.

How to test the pattern, step by step

  1. Open the Regex Tester and type the pattern, without slashes, in the Pattern field.
  2. Leave the Flags field empty for a single-address check. If you later test a block of text holding many addresses, add the g flag, because without it only the first match is reported.
  3. Paste or type a test string, starting with one that should pass, such as 192.168.0.1.
  4. Read the match list. Each match shows its text, its index (position) in the string and its capture groups. This pattern has no capture groups, so a match simply confirms the whole string fitted. The tester works the same way for simpler patterns: with \d+ and flag g against Order 123 ships in 45 days, it reports 123 at index 6 and 45 further along.
  5. Now test strings that should fail, and confirm the match list stays empty. A pattern fed only with passing examples is a guess, not a check.

The tester runs JavaScript's RegExp engine, whose rules differ slightly from the PCRE, Python or grep flavors. For this pattern the difference does not bite, but it is worth knowing before you trust a pattern blindly in a different environment.

Worked example: five test strings

Run these five strings through the pattern one at a time:

  • 192.168.0.1 matches. Four groups of one to three digits, three literal dots, nothing else.
  • 10.0.0.256 matches the simple pattern, even though 256 is not a legal octet. Hold on to this result; it is the limitation in action.
  • 999.999.999.999 also matches, for the same reason. Every group is three digits, and three digits is all the pattern asks for.
  • 192.168.1 does not match. It has only three groups, and the anchors refuse to let the pattern settle for part of the string.
  • 192.168.0.1.7 does not match either. Five groups is one too many, and again the anchors do the work.

Three passes and two failures, and one of the passes is a false friend. That is an honest summary of what the simple pattern is worth.

The stricter octet pattern

An octet from 0 to 255 can be described as alternatives: 25[0-5] covers 250 to 255, 2[0-4][0-9] covers 200 to 249, 1[0-9][0-9] covers 100 to 199, and [1-9]?[0-9] covers 0 to 99, allowing one or two digits. Joined with escaped dots and anchors, the full pattern becomes ^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$. Against this pattern, 192.168.0.1 still passes, while 10.0.0.256 and 999.999.999.999 fail.

It works, and you can verify every case in the tester. But look at it: the octet block repeats four times, a single typo in one copy breaks one position only, and the next developer has to decode it before daring to touch it. That maintenance cost is the real price of range checks in regex.

Limitations and alternatives

The simple pattern's limit is the one stated throughout this guide: it validates layout, not range, so numbers up to 999 pass. The strict pattern fixes the range but is long and fragile to edit. Neither pattern settles the separate question of leading zeros: 010.0.0.1 passes the simple pattern, and whether a leading zero is acceptable (some systems read it as octal) is a policy decision no pattern makes for you.

This is why most people validate in code instead. Split the string on dots, check there are exactly four parts, convert each part to a number and confirm it sits between 0 and 255. Four short lines any teammate can read, and the leading-zero rule takes one more. Use the regex where it is strong, pulling address-shaped strings out of logs or giving a form a first-pass check, and let code make the final call.

Two tester limits are worth knowing: results stop at 500 matches, and a pattern that matches the empty string is advanced safely so the tester never loops forever. An invalid pattern shows a plain-language error instead of a stack trace.

Common mistakes

  • Leaving the dot unescaped. 192.168.0.1 as a pattern matches 192x168y0z1 too, because . means any character. Always write \. between octets.
  • Forgetting the anchors. Without ^ and $, an address-shaped substring inside a longer string counts as a match, and validation quietly stops validating.
  • Trusting a match as proof of validity. A match from the simple pattern means "shaped like an address". Treating it as "is a real address" is how 999.999.999.999 ends up in a database.
  • Typing the pattern with slashes. The tester takes the pattern body only; slashes and trailing flags pasted from code produce an invalid pattern error or a pattern that matches nothing.
  • Testing only with addresses that pass. Always include a too-long, too-short and out-of-range string in your test set.

FAQs

Does the simple IPv4 pattern check that each number is 255 or less? No. The simple pattern uses \d{1,3} for each octet, which accepts any one, two or three digits, including 999. It verifies that the text looks like four numbers separated by dots. Range checking needs the stricter octet pattern or a few lines of code.

Why does 999.999.999.999 match the pattern? Because each 999 satisfies \d{1,3}: three digits is three digits. The pattern has no concept of 255 as a ceiling. This is the most common surprise with IPv4 regex, and the reason a matching result should be read as 'shaped like an address', not 'is a valid address'.

Do I type the pattern with slashes in the Regex Tester? No. Type just the pattern body, like \d{3}-\d{4}, and put any flags in their own field. Slashes belong to regex literals in code, not to the tester's Pattern field.

Why does the tester report only the first address in my text? Without the g flag, only the first match is reported, matching how most code uses a pattern. Add g in the Flags field when you want every match in the text listed.

Should I use the strict pattern or validate in code? For most projects, code is the better home for the range check: split on the dots, confirm four parts, and test that each part is a number from 0 to 255. The strict regex works, but it is long, easy to break while editing, and still leaves questions like leading zeros for you to decide separately.

Conclusion

An IPv4 regex is a shape test with an optional, expensive range test bolted on. Build it from \d{1,3} octets and escaped dots, anchor it, test it in the Regex Tester against strings that fail as well as strings that pass, and decide consciously where the 0 to 255 check will live. When you are ready to use capture groups to rewrite text rather than just match it, read How to Replace Text With Regex Capture Groups. More tools for this kind of work are collected in the Developer Tools archive.