Private IP Address (RFC 1918)
Match IPv4 addresses in the RFC 1918 private ranges: 10/8, 192.168/16, and 172.16/12.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("\\b(?:10(?:\\.\\d{1,3}){3}|192\\.168(?:\\.\\d{1,3}){2}|172\\.(?:1[6-9]|2\\d|3[01])(?:\\.\\d{1,3}){2})\\b", "g");
const input = "Servers at 10.0.0.5 and 192.168.1.20";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"\b(?:10(?:\.\d{1,3}){3}|192\.168(?:\.\d{1,3}){2}|172\.(?:1[6-9]|2\d|3[01])(?:\.\d{1,3}){2})\b")
input_text = "Servers at 10.0.0.5 and 192.168.1.20"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\b(?:10(?:\.\d{1,3}){3}|192\.168(?:\.\d{1,3}){2}|172\.(?:1[6-9]|2\d|3[01])(?:\.\d{1,3}){2})\b`)
input := `Servers at 10.0.0.5 and 192.168.1.20`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
\b(?:10(?:\.\d{1,3}){3}|192\.168(?:\.\d{1,3}){2}|172\.(?:1[6-9]|2\d|3[01])(?:\.\d{1,3}){2})\b (flags: g)Raw source: \b(?:10(?:\.\d{1,3}){3}|192\.168(?:\.\d{1,3}){2}|172\.(?:1[6-9]|2\d|3[01])(?:\.\d{1,3}){2})\b
How it works
Examples
Input
Servers at 10.0.0.5 and 192.168.1.20Matches
10.0.0.5192.168.1.20
Input
172.16.50.1 is private; 172.32.0.1 is notMatches
172.16.50.1
Input
Public IP 8.8.8.8No match
—Common use cases
- •Filtering log files for internal vs external traffic
- •Auditing firewall rules for unexpected public IPs
- •Network configuration validation
- •Telemetry redaction (drop external IPs from internal logs)
Related patterns
IP Address with CIDR Notation
NetworkingMatch IPv4 addresses with CIDR prefix notation such as 10.0.0.0/8 or 192.168.1.0/24.
IPv4 Address
NetworkingMatch valid IPv4 addresses with each octet constrained to 0–255.
Hostname (Single Label, RFC 1123)
NetworkingValidate a single-label hostname per RFC 1123: 1–63 chars, letters/digits/hyphens, can't start or end with hyphen.
IPv6 Address
NetworkingMatch full (non-compressed) IPv6 addresses written as eight colon-separated hextets.