I am basically trying to match a 10 character ISBN, so far I am able to match any string that is 10 characters long but it is not accurate at identifying the string as an ISBN
A 10 character long isbn can have 9 starting digits and end with a letter or have 10-digits, e.g.
0273737025
027373702X
If the final character is a letter it will always be X
what I have so far
[a-zA-Z0-9]{10,10}
this regex will be able to extract an isbn from a string like
"asjdh - asd a - dsa- 0273737025" = 0273737025
but will also extract anything else that is 10 or more characters long
"asjdh - asd a - dsa- myveryearly" = myveryearl
Is there a regular expression that can meet these requirements?
\d{9}(?:\d|X)
This is 9 digits followed by either a digit or 'X'.
This should work:
[0-9]{9}[xX0-9]
Or a more concise form:
\d{9}[xX\d]
You will have to use a lookbehind to prevent finding 9 digits that were preceded by more digits.
(?<!\d)\d{9}[\dxX]