首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ISBN-10和ISBN-13扩展到非英语国家

将ISBN-10和ISBN-13扩展到非英语国家
EN

Stack Overflow用户
提问于 2021-01-27 18:21:18
回答 2查看 116关注 0票数 1

我正在寻找一个Regex来匹配一个字符串,该字符串应该:

  • 从可选的978-979-前缀开始
  • 继续用一个数字
  • 有确切的7位和2个连字符的排列,没有连续的连字符。
  • 以数字、连字符、数字(可以是x)的序列结尾。

注:连字符可以用空格替换(连贯),也可以完全不存在。

比赛:

  • 9780309091695
  • 0309091695
  • 97903030909169x
  • 030909169 x
  • 988-0-309-09169-5
  • 1-84356-028-3
  • 978 0 8044 2957 7
  • 93 86954 21 4
  • 979-0-943396-04-2
  • 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

不匹配:

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

我目前的观点是,多亏了以前的问题的答案,我应该能够进一步简化:/^((97(8|9))*\d{9}|(97(8|9)-)*(?=.{11}-)(?:\d+-){3}|(97(8|9) )*(?=.{11} )(?:\d+ ){3})[\dx]$/i

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-27 21:02:09

你可以尝试:

代码语言:javascript
复制
^(?![- ])(?:(?:97[89])?([- ]?)(?=(?:\d\1?){9}\1[\dxX]$)(?:\d+\1){3}[\dxX]|(?=.{11}([- ])[\dxX]$)(?:\d+\2){3}[\dxX])

请参阅在线演示

  • ^ -启动字符串锚.
  • (?![- ]) -负向前看,以防止前导连字符或空格。
  • (?: -开放非捕获组:
    • (?: -开放非捕获组:
      • 97[89] -匹配"97“,然后是8或9。
      • )? -关闭非捕获组,并使其可选。

代码语言:javascript
复制
- `(` - Open 1st capture group: 
    - `[- ]?` - Optionally match an hyphen or space.
    - `)` - Close 1st capture group.
代码语言:javascript
复制
- `(?=` - Open positive lookahead: 
    - `(?:` - Open non-capture group: 
        - `\d\1?` - 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).
代码语言:javascript
复制
    - `\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.
代码语言:javascript
复制
- `(?:` - 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.
代码语言:javascript
复制
- `[\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.
代码语言:javascript
复制
- `(?:` - 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.
代码语言:javascript
复制
- `[\dxX]$` - Match a digit, a lower- or uppercase "x".
- `)` - Close non-capture group.
票数 2
EN

Stack Overflow用户

发布于 2021-01-27 21:06:50

此值既匹配又不匹配978 0 309 09169 5。我认为应该匹配它,因为它具有相同的前缀、10位数字和相同数量的分隔符。

另一个选项可以是使用交替|将可选前缀和10位数字或部分与分隔符匹配。

您可以使用([- ])?捕获组1中的可选空格或连字符,并使用反向引用\1来引用它,以保持分隔符的一致性。

代码语言:javascript
复制
^(?:97[89]([- ])?)?(?:\d{10}|(?=(?:\d\1?){9}\1?[xX\d]$)\d+(?:\1?\d+){2}\1?[\dxX])$

解释

  • ^起刺
  • (?:97[89]([- ])?)?可选择匹配组1中的前缀和捕获可选连字符。
  • (?:非捕获群
    • \d{10}匹配10位数字
    • |
    • (?=(?:\d\1?){9}\1?[xX\d]$)正向前看,断言9个数字,后面跟着一个可选的分隔符,或者是x X或一个数字,直到字符串的末尾
    • \d+(?:\1?\d+){2}匹配1+数字并重复2次,匹配可选分隔符和1+数字。
    • -?[\dxX]匹配可选的-x X或数字

  • )闭非捕获群
  • 字符串的$末端

Regex演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65925071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档