Regex for IPv4 Address with Port
Regex Pattern
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}$IPv4 address followed by colon and port number
Quick Answer
The regex pattern for ipv4 address with port is `^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}$`. IPv4 address followed by colon and port number. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 192.168.1.1:8080 | ✓ Matches |
| 10.0.0.1:3000 | ✓ Matches |
| 127.0.0.1:443 | ✓ Matches |
| 192.168.1.1 | ✗ No match |
| 256.0.0.1:80 | ✗ No match |
| localhost:3000 | ✗ No match |
Code Examples
javascript
const regex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}$/;
const isValid = regex.test(value);python
import re
pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\d{1,5}$/', $value)) {
echo "valid";
}java
String pattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\\d{1,5}$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
IPv4 Address
Validates a well-formed IPv4 address (0-255 in each octet)
IPv6 Address
Basic IPv6 address (8 groups of 4 hex digits)
MAC Address
MAC address with colons or dashes as separators
IPv4 CIDR Notation
IPv4 address with CIDR subnet mask (e.g. 10.0.0.0/24)
Private IP Address
RFC 1918 private IPv4 address ranges (10.x, 172.16-31.x, 192.168.x)
Localhost Detection
Detects localhost URLs (localhost, 127.0.0.1, ::1)