space ranger
space ranger

Reputation: 422

Regex error ISBN 10

i have the following regular expression to use for ISBN-10

^[0-9]{9}[[0-9]|X|x]$^

it seems to work fine with all numbers, however when letters are introduced it gives me the letter instead, example ISBN 047146158X returns X in the array,

can someone help me with my expression?

this is what the data looks like

startISBN 10-047146158X Author(s): Stephen R. Bolsover, Jeremy S. Hyams, Elizabeth A. Shephard, Hugh A. White, Claudia G. Wiedemann Publisher- Wiley 27 JAN 2004

Upvotes: 1

Views: 337

Answers (2)

Darshana
Darshana

Reputation: 2548

try this pattern

\b[0-9]{9}[0-9Xx]\b

Upvotes: 1

JJJ
JJJ

Reputation: 33163

Either

[0-9]{9}([0-9]|X|x)

or just

[0-9]{9}[0-9Xx]

The [] brackets match a single character inside them, nesting them doesn't do what you want. Parentheses () mark a group where you can use the | character to separate different options, but since you're looking for one character only you can just put everything in brackets.

^ means "start of line" and $ means "end of line" - if you have them at the start and at the end, the regex matches only if there's nothing else in the input.

Upvotes: 2

Related Questions