rodrigosantosbr

Regex Ip Adress

Jan 15th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

Site:
https://regex101.com/
https://www.jdoodle.com/python3-programming-online
https://playcode.io/online-javascript-editor


This matches an IP address, putting each number in its own group that can be retrieved by number.
If you do not care about capturing the numbers, then you can make this shorter by putting everything after ^ until immediately after the first . in a group ( ) with a {3} after it. Then put the number matching regex in once more.

Expression:

^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$

Example text:

200.17.33.1
256.256.256.256
255.255.255.255
254.254.254.254
0.0.0.0
aaaaa
a.a.a.a

Javascript

https://playcode.io/online-javascript-editor

var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
var term1 = "200.17.33.1";
var term2 = "256.256.256.256";
var term3 = "0.0.0.0";

if (re.exec(term1)) {
    console.log("term1 Valid");
}
else {
    console.log("term1 Invalid");
}

if (re.exec(term2)) {
    console.log("term2 Valid");
}
else {
    console.log("term2 Invalid");
}

if (re.exec(term3)) {
    console.log("term3 Valid");
}
else {
    console.log("term3 Invalid");
}
Add Comment
Please, Sign In to add comment