tags:

views:

351

answers:

6

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

A: 

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.

Bart Kiers
that'll match 10 to 13 digits, he needs either 10 or 13. isbn cannot be 11 or 12
SilentGhost
You're absolutely right.
Bart Kiers
A: 

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

Mark
A: 
/^\/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})\/$/
ianhales
See SilentGhost's earlier comment in my answer: *"that'll match 10 to 13 digits, he needs either 10 or 13. isbn cannot be 11 or 12"*.
Bart Kiers
True. Will edit to include an OR.
ianhales
A: 

In Python you colud use:

^/book/([a-zA-Z0-9-]+)/(\d{10,13})/$
See SilentGhost's earlier comment in my answer: "that'll match 10 to 13 digits, he needs either 10 or 13. isbn cannot be 11 or 12".
Bart Kiers
You are correct.
+2  A: 
/^\/book\/([a-zA-Z0-9\-]+)\/(\d{10}|\d{13})\/$/

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

tig
A: 
/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
PintSizedCat