您的请求不明确: 1)你正在寻找 字母'a''的第一个,也是唯一的序列。如果第一个,而且只有第一个字母a的第一个,b $ b序列的长度不是3,则根本不匹配。 2)你正在寻找第一个,也是唯一一个 长度为3的字母a的序列。 $ b $那是什么? Sybren - 世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉 安全标签来解决问题呢? br /> Frank ZappaYour request is ambiguous:1) You''re looking for the first, and only the first, sequence of theletter ''a''. If the length of this first, and only the first,sequence of the letter ''a'' is not 3, no match is made at all.2) You''re looking for the first, and only the first, sequence oflength 3 of the letter ''a''.What is it?Sybren--The problem with the world is stupidity. Not saying there should be acapital punishment for stupidity, but why don''t we just take thesafety labels off of everything and let the problem solve itself?Frank Zappa 我不太清楚你的意图是什么,因为 结果发现显然是aaa,长度为3. 如果你的意思是你要测试一些 物品,只找到aaa的物品。是第一个a在 这行,你可能会尝试类似 import re listOfStringsToTest = [ ''helloworld'', ''xyz123aaabbaabababbab'', ''cantalopeaaabababa'', ''baabbbaaabbbbb'', ''xyzaa123aaabbabbabababaa''] r = re.compile(" [^ a] *(a {3})b +(a + b +)*") 匹配= [s for listOfStringsToTest中的s,如果r.match(s)] print repr(匹配) 如果你只是想要aaa的* first * triad,你可以将正则表达式改为 r = re.compile("。*?(a) {3})b +(a + b +)*") 关于问题要点的更多细节, 或许更好解决方案可以找到。特别是 listOfStringsToTest中的项目应该找到 但是没有任何一个正则表达式? -tkcI''m not quite sure what your intent here is, as theresulting find would obviously be "aaa", of length 3.If you mean that you want to test against a number ofthings, and only find items where "aaa" is the first "a" onthe line, you might try something likeimport relistOfStringsToTest = [''helloworld'',''xyz123aaabbaabababbab'',''cantalopeaaabababa'',''baabbbaaabbbbb'',''xyzaa123aaabbabbabababaa'']r = re.compile("[^a]*(a{3})b+(a+b+)*")matches = [s for s in listOfStringsToTest if r.match(s)]print repr(matches)If you just want the *first* triad of "aaa", you can changethe regexp tor = re.compile(".*?(a{3})b+(a+b+)*")With a little more detail as to the gist of the problem,perhaps a better solution can be found. In particular, arethere items in the listOfStringsToTest that should be foundbut aren''t with either of the regexps?-tkc 这篇关于正则表达式匹配第一个序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!