简体   繁体   English

Python 正则表达式 - 带有 [AZ] 和 DIGITS [\\d] 的字符串

[英]Python Regex - STRING with [A-Z] and DIGITS [\d]

import re 

 str_1 = "ABABABABABAabababaacdcdcdcd"
 str_2 = "ABABABABAB1ab2babaacdcdcdcd"
 str_3 = "123456A"
 str_4 = "123456"

 #str_3Plus_d = re.search(r'(?=.*?\d)[A-Z\d]{10,}',str_2).group(0)
 str_3Plus_d = re.search(r'(?=.*?\d)[A-Z\d]{2,4}',str_2).group(0)
 print(str_3Plus_d)

 #str_3plus_a = re.search(r'(?=.*?\d)(?=.*?[A-Z])[A-Z\d]{3,}',str_4).group(0)

 #str_3plus_both = re.search(r'(?=.*?\d)(?=.*?[A-Z])[A-Z\d]{3,}',str_4).group(0)

I am stuck with searching a STRING ([AZ] and DIGITS).我一直在搜索字符串([AZ] 和数字)。 I searched SO for similar Questions , here -- https://stackoverflow.com/a/28334645/4928635 , it says that (?=.*?\\d) "Checks for atleast one digit" , but that doesnt seem to be the case.我在这里搜索了类似的问题 - https://stackoverflow.com/a/28334645/4928635 ,它说(?=.*?\\d) “检查至少一位数字”,但这似乎不是案子。 As seen in -正如在 -

str_3Plus_d = re.search(r'(?=.*?\d)[A-Z\d]{2,4}',str_2).group(0)
print(str_3Plus_d)

it prints me a string - which has no digits ?它给我打印了一个没有数字的字符串? String it print in my JupyterNotebook is as seen below -它在我的 JupyterNotebook 中打印的字符串如下所示 -

 ABAB

How to capture a string with - "variable number of characters and at least 2 digits and at most 4 digits , as an example - how to catch - AAABBB2223" ...如何捕获带有 - “可变数量的字符和至少 2 位数字,最多 4 位数字的字符串,例如 - 如何捕获 - AAABBB2223”...

Further clarifications --进一步说明——

  1. @pm-2ring :- the digits and alpha characters can occur in any order , the substring im trying to capture can vary in length. @pm-2ring :- 数字和字母字符可以以任何顺序出现,我试图捕获的子字符串的长度可能会有所不同。

  2. @jean-françois-fabre :- you seem to have removed your answer - the code you gave did exactly what u said it would - thanks . @jean-françois-fabre :-您似乎已经删除了您的答案-您提供的代码完全符合您的要求-谢谢。

  3. @clasg :- Both your comment and answer have taught me well today - im very grateful - i will revert in sometime and let you know if i would want to upvote your answer - many thanks. @clasg :- 今天你的评论和回答都教会了我很好 - 我非常感激 - 我会在某个时候回复并让你知道我是否想支持你的回答 - 非常感谢。

You can do this by moving the quantification into the look-ahead and some...您可以通过将量化移动到前瞻性和一些...

^(?=(?:[^\d\n]*\d){2,4}[^\d\n]*$)[A-Z\d]*$

It's a bit more complex, but does the work, as it should.它有点复杂,但可以正常工作。

The [^\\d\\n] matches anything but digits or newlines. [^\\d\\n]匹配除数字或换行符以外的任何内容。 Making the positive look-ahead match this construct any number of times ( * ), followed by a digit, then repeating this 2-4 times, finally followed by the non digit/LF again , makes it only match a string if it contains between 2 and 4 digits.使正前瞻匹配这个构造任意次数( * ),后面跟着一个数字,然后重复2-4次,最后接着非数字/ LF再次,使得只有当它包含之间匹配的字符串2 位和 4 位数字。

See it here at regex101 .在 regex101 上看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM