开发者

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


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

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


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


In Python you colud use:

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


/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


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.


/^\/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})\/$/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜