Regex for X-Forwarded-For Header
Regex Pattern
^(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:,\s*)?)+$X-Forwarded-For header with comma-separated IPs
Quick Answer
The regex pattern for x-forwarded-for header is `^(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:,\s*)?)+$`. X-Forwarded-For header with comma-separated IPs. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 192.168.1.1 | ✓ Matches |
| 10.0.0.1, 172.16.0.1 | ✓ Matches |
| 8.8.8.8, 192.168.1.1, 10.0.0.1 | ✓ Matches |
| not-an-ip | ✗ No match |
| 192.168.1 | ✗ No match |
| abc.def.ghi.jkl | ✗ No match |
Code Examples
javascript
const regex = /^(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:,\s*)?)+$/;
const isValid = regex.test(value);python
import re
pattern = r'^(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:,\s*)?)+$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:,\s*)?)+$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:,\s*)?)+$/', $value)) {
echo "valid";
}java
String pattern = "^(?:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(?:,\\s*)?)+$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
Subdomain Extraction
Extracts the subdomain from a URL or domain name
Protocol Detection
Detects and captures the protocol from a URL
Port Number
Extracts port number from a URL or host string
Query Parameter
Extracts key-value pairs from URL query strings
HTTP vs HTTPS URL
Matches HTTP or HTTPS URLs and captures the protocol
Data URL
Matches data: URL scheme with optional MIME type and base64 encoding