Caesar
Caesar

Reputation: 91

How to match book title and isbn with regex?

Hi I am looking into writing a regular expression that will match the following

/book/The-title-of-the-book/0000000000/

where The-title-of-the-book is any alphanumeric character and a '-', 0000000000000 is the 10 or 13 digit isbn of the book, e.g. /book/To-kill-a-mockingbird/0099419785/.

can you suggest what pattern I may use to construct the regular expression

Upvotes: 0

Views: 2567

Answers (6)

tig
tig

Reputation: 27790

/^\/book\/([a-zA-Z0-9\-]+)\/(\d{10}|\d{13})\/$/

also you don't need ^ and $ if you want to search text for this string

Upvotes: 3

n00dle
n00dle

Reputation: 6043

/^\/book\/[a-zA-Z0-9-]+\/\d{10,13}\/$/

Not tested but I believe that should match what you want.

EDIT: This should make it so the ISBN is either 10 or 13 characters, not in-between.

/^\/book\/[a-zA-Z0-9-]+\/(\d{10}|\d{13})\/$/

Upvotes: 0

Bart Kiers
Bart Kiers

Reputation: 170148

Try:

/book/([a-zA-Z\d-]+)/(\d{10}|\d{13})/

Note that in some languages, the / is used to delimit the regex, in which case you'll need to escape the / in it:

/\/book\/([a-zA-Z\d-]+)\/(\d{10}|\d{13})\//

Or use another delimiter, if possible.

The book will be captured in group 1, the isbn in group 2.

Upvotes: 0

Henry B
Henry B

Reputation: 8047

/book/[a-zA-Z0-9-]+/([0-9]{10}|[0-9]{13})/

This does exactly what you want

  • [a-zA-Z0-9-]+ : Tests for one or more alphanumeric characters
  • ([0-9]{10}|[0-9]{13}) : Tests for 10 digits or 13 digits

Upvotes: 0

lutz
lutz

Reputation:

In Python you colud use:

^/book/([a-zA-Z0-9-]+)/(\d{10,13})/$

Upvotes: 0

mmmmmm
mmmmmm

Reputation: 32661

In this case I would not use regexes and just split the string on / the the last part is the ISBN and the second last the title.

I would refer to the comment

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

from Jaime Zawinski

Upvotes: 0

Related Questions