Python正则表达式匹配,如果字符串中有X个以@#$开头的单词:
我要做的是匹配字符串,如果该字符串包含X个以@#$:字符开头的单词(比如5个) 假设X为5的示例:Python正则表达式匹配,如果字符串中有X个以@#$开头的单词:,python,regex,Python,Regex,我要做的是匹配字符串,如果该字符串包含X个以@#$:字符开头的单词(比如5个) 假设X为5的示例: @someword someotherword anotherword word1 word2 word3 => false @someword :someotherword #anotherword $word1 word2 word3 => false @someword :someotherword #anotherword $word1 #word2 $word3 =>
@someword someotherword anotherword word1 word2 word3 => false
@someword :someotherword #anotherword $word1 word2 word3 => false
@someword :someotherword #anotherword $word1 #word2 $word3 => true
像这样的
import re
my_re = '[#@$:][a-zA-Z]*'
my_string = "#hello :my #name $is $stef"
print(len(re.findall(my_re,my_string)) >= 5)
像这样的
import re
my_re = '[#@$:][a-zA-Z]*'
my_string = "#hello :my #name $is $stef"
print(len(re.findall(my_re,my_string)) >= 5)
积极的前瞻是实现这一点的一种方法:
input = "@someword :someotherword #anotherword $word1 #word2 $word3"
result = re.match(r'.*((?<=\s)|(?<=^))[@#$:]\S+.*(\s[@#$:]\S+.*){4}', input)
if result:
print("Found a match")
input=“@someword:someotherword#另一个单词$word1#word2$word3”
result=re.match(r'.*((?积极的前瞻是实现这一点的一种方法:
input = "@someword :someotherword #anotherword $word1 #word2 $word3"
result = re.match(r'.*((?<=\s)|(?<=^))[@#$:]\S+.*(\s[@#$:]\S+.*){4}', input)
if result:
print("Found a match")
input=“@someword:someotherword#另一个单词$word1#word2$word3”
result=re.match(r'.*(?正确的正则表达式是((?:[@#$].+){5})
例如:
import re
...
tst = """
@someword someotherword anotherword word1 word2 word3
@someword :someotherword #anotherword $word4 #word5 $word6
@someword :someotherword #anotherword $word1 word2 word3
@someword :someotherword #anotherword $word1 #word2 $word3
"""
res = re.findall(r"((?:[@#$].+){5})", tst)
print(res)
结果:
['@someword :someotherword #anotherword $word4 #word5 $word6', '@someword :someotherword #anotherword $word1 #word2 $word3']
正确的正则表达式是((?:[@#$].+){5})
例如:
import re
...
tst = """
@someword someotherword anotherword word1 word2 word3
@someword :someotherword #anotherword $word4 #word5 $word6
@someword :someotherword #anotherword $word1 word2 word3
@someword :someotherword #anotherword $word1 #word2 $word3
"""
res = re.findall(r"((?:[@#$].+){5})", tst)
print(res)
结果:
['@someword :someotherword #anotherword $word4 #word5 $word6', '@someword :someotherword #anotherword $word1 #word2 $word3']
假设这些符号仅在单词字符之前使用,则可以使用此正则表达式:
(?:]\B[@#$:]\w+[^@#$:]*){5}
代码:
>>> arr = ['@someword someotherword anotherword word1 word2 word3',
'@someword :someotherword #anotherword $word1 word2 word3',
'@someword :someotherword #anotherword $word1 #word2 $word3']
>>> reg = re.compile(r'(?:\B[@#$:]\w+[^@#$:\n]*){5}');
>>> for i in arr:
... print(reg.findall(i))
...
[]
[]
['@someword :someotherword #anotherword $word1 #word2 ']
\B
:匹配\B
不匹配的位置
[@#$:]\w+
:匹配1个以上以[@#$:]开头的单词字符
[^@$:]*
:匹配0个或多个不包含[@$:]的字符。
(…){5}
:在当前输入中匹配其中的5个
如果这些符号仅在单词字符之前使用,则可以使用此正则表达式:
(?:]\B[@#$:]\w+[^@#$:]*){5}
代码:
>>> arr = ['@someword someotherword anotherword word1 word2 word3',
'@someword :someotherword #anotherword $word1 word2 word3',
'@someword :someotherword #anotherword $word1 #word2 $word3']
>>> reg = re.compile(r'(?:\B[@#$:]\w+[^@#$:\n]*){5}');
>>> for i in arr:
... print(reg.findall(i))
...
[]
[]
['@someword :someotherword #anotherword $word1 #word2 ']
\B
:匹配\B
不匹配的位置
[@#$:]\w+
:匹配1个以上以[@#$:]开头的单词字符
[^@$:]*
:匹配0个或多个不包含[@$:]的字符。
(…){5}
:在当前输入中匹配其中的5个
首先,谢谢。代码方面,是的。但我需要它作为正则表达式,因为我使用多个正则表达式,需要插入直接正则表达式。但不确定这是否可能。我不认为仅使用正则表达式就可以得到真或假。例如,你不允许使用re.match?@vanderZonStef是的,你可以,使用非常难看的正则表达式模式:P@TimBiegeleisen检查我下面的答案,这是一个简单的模式,绝对不难看。首先,谢谢。代码方面,是的。但我需要它作为一个正则表达式,因为我使用多个正则表达式,需要插入直接正则表达式。但不确定是否可能。我不认为只有正则表达式就可以得到正确或错误。例如,你不允许使用re.match?@vanderzonsteyes、 您可以使用非常难看的正则表达式模式:P@TimBiegeleisen检查我下面的答案,这是一个简单的模式,绝对不难看。我也测试了它,它可以按我想要的方式工作,谢谢!:)我也测试了它,它可以按我想要的方式工作,谢谢!:)问题是,如果你在一个单词中使用了多个标记,它仍然会匹配。而且,我认为你根本没有处理单词边界。不会a@someword
是否也使用您的逻辑进行匹配?@Latyos您没有提到提供的符号不应位于除开始之外的任何其他位置。无论如何,使用3个小符号编辑将修复它;)问题是,如果你在一个单词中使用了多个标记,它仍然会匹配。而且,我认为你根本没有处理单词边界。不会a@someword
是否也使用您的逻辑进行匹配?@Latyos您没有提到提供的符号不应位于除开始之外的任何其他位置。无论如何,使用3个小符号编辑会解决它;)我支持你,兄弟,你的答案+1:-)谢谢Tim。实际上我一开始就有了\B
,但regex101调试器遇到了灾难性的回溯,所以为了演示目的我删除了它,并在这里复制/粘贴了不正确的regex。在python中,它运行得非常好。是的……我也很早就放弃了regex101,因为我也遇到了问题。我得到了你的支持,兄弟,+1对于你的回答:-)谢谢蒂姆。实际上,我一开始就有\B
,但regex101调试器遇到了灾难性的回溯,所以为了演示,我删除了它,并在这里复制/粘贴了不正确的regex。在python中,它运行得很好。是的……我也很早就放弃了Regex 101,因为我也遇到了问题。