Avatar of arashaga
arashaga
 asked on

Regular expression for isbn

I am not really good at regular expression and I was trying to come up with a regular expression that could validate the isbn numbers in my php code,
I will strip the dashes and spaces from the string so I will make sure that the string has only the digits plus that optional x or X. I was wondering if one you regular expression gurus out there could help me figure this out.

Thank you,
Regular ExpressionsPHPPerl

Avatar of undefined
Last Comment
arashaga

8/22/2022 - Mon
theremon

Hi there - are you looking for this?

^ISBN\s(?=[-0-9xX ]{13}$)(?:[0-9]+[- ]){3}[0-9]*[xX0-9]$
theremon

Forgot to mention that the pattern above includes dashes, so you don't need to clean them out.
theremon

There are several other methods of course. If you really, really only need those numbers-plus-X then you can use:
^[0-9]{9}[0-9xX]$
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
arashaga

ASKER
Thank you theremon but the problem is it doesn't validate the "9781430224594" which is 13digits . as for the dashes and spaces I will take care of them myself . just a plain isbn with 10 or 13 digits and optional x or X
theremon

Ok, give me a minute...
ASKER CERTIFIED SOLUTION
theremon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
arashaga

ASKER
Thanks theremon it seems like it's working!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.