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})?$"
LVL 2
x95guzikAsked:
Who is Participating?
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.