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,

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,

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

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]$

^[0-9]{9}[0-9xX]$

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

Ok, give me a minute...

ASKER CERTIFIED SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER

Thanks theremon it seems like it's working!

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