• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

regex IP validator

Below is a regex for validating an ip address.  I need to modify it to accept a x instead of numbers at any point in the entry.

Example:
want it to accept 222.123.213.54 and also accept 222.123.x.54 ect where the x can replace any one set of numbers.  Thanks in advance and more power to the folks who understand regex as it is like a foreign language to me.

"^((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))[\\.]){3}(?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))(\\:[0-9]{1,5})?$"
0
x95guzik
Asked:
x95guzik
  • 4
  • 2
  • 2
1 Solution
 
star_trekCommented:
"^((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))[\\.]){2}((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1})|(x))[\\.]){1}(?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))(\\:[0-9]{1,5})?$"
0
 
RoonaanCommented:
if(preg_match('/^222\.123\.\d{1,3}\.54$/', $myIPString)) {
  //valid
} else {
  //invalid.
}

-r-
0
 
x95guzikAuthor Commented:
thanks star_trek  One last thing is that I want x to substituable for any of the last three.  I am trying figure it out form what you sent but am still fighting through.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
star_trekCommented:
If I understand properly,  then here is the following "^((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))[\\.]){2}((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1})|(x))[\\.]){1}(?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1})|(x))(\\:[0-9]{1,5})?$"
0
 
star_trekCommented:
Can you give an example of what exactly you want, in above i substituted x for the fourth string after the dot
222.123.213.54  or 222.123.x.54 or 222.123.x.x or 222.123.213.x

let me know if that is waht you wanted from your request.
0
 
x95guzikAuthor Commented:
192.x.12.12 - 192.12.x.12 - 192.12.12.x - 192.x.x.12 - 192.12.x.x would all be valid.
and if it is easier no problems with 192.x.x.x would be ok to.  So basically all can be x except the first one
0
 
star_trekCommented:
try this
"^((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1}))[\\.]){1}((?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1})|(x))[\\.]){2}(?:([2]{1}[0-5]{2})|([2]{1}[0-4]{1}[0-9]{1})|([1]?[0-9]{2})|([0-9]{1})|(x))(\\:[0-9]{1,5})?$"
0
 
RoonaanCommented:
You can do this without regexp:

<?php

function matchPattern($pattern, $ip) {
  $parts1 = explode('.', $pattern);
  $parts2 = explode('.', $ip);
  foreach($parts1 as $index => $key) {
     if($parts2[$index] != $key && $key != 'x') return false;
  }
  return true;
}

if(matchPattern('192.x.x.x', '192.1.2.3')) {
  echo 'matches';
} else {
  echo 'doesn\'t match';
}
?>

Kind regards

-r-
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now