DD
DevDash

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

InputResult
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"
end

php

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

Want API access + no ads? Pro coming soon.