匹配以9位开头并可能以字符串结尾的字符串

匹配以9位开头并可能以字符串结尾的字符串

问题描述:

I am basically trying to match a 10 character ISBN, so far I am able to match any string that is 10 characters long but it is not accurate at identifying the string as an ISBN

A 10 character long isbn can have 9 starting digits and end with a letter or have 10-digits, e.g.

0273737025

027373702X

If the final character is a letter it will always be X

what I have so far

[a-zA-Z0-9]{10,10}

this regex will be able to extract an isbn from a string like

"asjdh - asd a -  dsa- 0273737025"  = 0273737025

but will also extract anything else that is 10 or more characters long

"asjdh - asd a -  dsa- myveryearly"  = myveryearl

Is there a regular expression that can meet these requirements?

我基本上试图匹配10个字符的ISBN,到目前为止我能够匹配任何10个字符的字符串 但是将字符串识别为ISBN不准确 p>

10个字符长的isbn可以有9个起始数字,以字母结尾或有10个数字, e.g。 p>

0273737025 p>

027373702X p>

如果最后一个字母是一个字母,它将始终是 X code> p>

到目前为止 strong> p>

  [a-zA-Z0-9]  {10,10} 
  code>  pre> 
 
 

此正则表达式将能够从 p>

 “这样的字符串中提取isbn  asjdh  -  asd a  -  dsa- 0273737025“= 0273737025 
  code>  pre> 
 
 

但也会提取任何长度为10个或更多字符的内容 p> “asjdh - asd a - dsa- myveryearly”= myveryearl code> pre>

是否有正则表达式可以满足这些要求? p> \ n div>

\d{9}(?:\d|X)

This is 9 digits followed by either a digit or 'X'.

This should work:

[0-9]{9}[xX0-9]

Or a more concise form:

\d{9}[xX\d]

You will have to use a lookbehind to prevent finding 9 digits that were preceded by more digits.

(?<!\d)\d{9}[\dxX]