Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

Regex to extend ISBN-10 and ISBN-13 to non-English speaking countries

I am looking for a Regex to match a string which should:

  • start with an optional 978- or 979- prefix
  • continue with a digit
  • have a permutation of exactly 7 digits and 2 hyphens, without 2 consecutive hyphens
  • end with a sequence of digit, hyphen, digit (which can be an x)

NB.: Hyphens can be replaced (coherently) by spaces or not present altogether...

Match:

  • 9780309091695
  • 0309091695
  • 979030909169x
  • 030909169x
  • 978-0-309-09169-5
  • 1-84356-028-3
  • 978 0 8044 2957 7
  • 93 86954 21 4
  • 979-0-943396-04-2
  • 0-9752298-0-X
  • 979 99921 58 10 0
  • 80 902734 1 x
  • 979-9971-5-0210-X
  • 978-425-059-0
  • 979 85 359 0277 x

No Match:

  • 978030909169
  • 97803090916951
  • 030909169
  • 03090916951
  • 978-0--30909169-5
  • 978 0 30909169 5
  • 978 0-309 09169-5
  • 978-0-309-091695-
  • -1-84356-028-3
  • 978-0309091695
  • 978-0-30909169-5
  • 1-843-56-028-3

My current proposition is thanks to answers I had to a previous question and should be able to be further simplified: /^((97(8|9))*d{9}|(97(8|9)-)*(?=.{11}-)(?:d+-){3}|(97(8|9) )*(?=.{11} )(?:d+ ){3})[dx]$/i

question from:https://stackoverflow.com/questions/65925071/regex-to-extend-isbn-10-and-isbn-13-to-non-english-speaking-countries

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may try:

^(?![- ])(?:(?:97[89])?([- ]?)(?=(?:d1?){9}1[dxX]$)(?:d+1){3}[dxX]|(?=.{11}([- ])[dxX]$)(?:d+2){3}[dxX])

See the online demo

  • ^ - Start string anchor.
  • (?![- ]) - Negative lookahead to prevent a leading hyphen or space.
  • (?: - Open non-capture group:
    • (?: - Open non-capture group:
      • 97[89] - Match "97" followed by either an 8 or 9.
      • )? - Close non-capture group and make it optional.
    • ( - Open 1st capture group:
      • [- ]? - Optionally match an hyphen or space.
      • ) - Close 1st capture group.
    • (?= - Open positive lookahead:
      • (?: - Open non-capture group:
        • d1? - Capture a digit and optionally match what is captured in the 1st capture group.
        • ){9} - Close the non-capture group and match it nine times (to assert a position with 9 digits ahead).
      • 1[dxX]$ - Again match what is captured in the 1st capture group followed by a digit or lower- or uppercase "x" and the end string anchor.
      • ) - Close positive lookahead.
    • (?: - Open non-capture group:
      • d+1 - 1+ digits followed by what is captured in the 1st capture group.
      • ){3} - Close the non-capture group and match it three times.
    • [dxX] - Match a digit, a lower- or uppercase "x".
    • | - Or:
    • (?= - Open positive lookahead:
      • .{11} - Match eleven characters other than newline.
      • ([- ]) - OA 2nd capture group to match either hypen or space.
      • [dxX]$ - Match a digit, a lower- or uppercase "x" up to end string anchor.
      • ) - Close positive lookahead.
    • (?: - Open non-capture group:
      • d+2 - Match 1+ digits and what is captured in the 2nd capture group.
      • ){3} - Close non-capture group and match three times.
    • [dxX]$ - Match a digit, a lower- or uppercase "x".
    • ) - Close non-capture group.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...