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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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})?$"
RoonaanCommented:
if(preg_match('/^222\.123\.\d{1,3}\.54$/', $myIPString)) {
  //valid
} else {
  //invalid.
}

-r-
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.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

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})?$"
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.
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
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})?$"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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-
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.