我需要匹配双字,但我的正则表达式无法正常工作。
L = "let's s?,play%3with,1symbols88,/symbols"
pattern = r'(\b\S+\b)[\d\s\.,-?\)\(!\/]+\b\1\b'
r = re.compile(pattern, re.IGNORECASE)
print(re.findall(r, L))
# Outputs: ['s']
# Expected: ['s','symbols']
您可以使用更基本的正则表达式Counter
:
import re
from collections import Counter
text = "let's s?,play%3with,1symbols88,/symbols"
word_pattern = re.compile('[a-z]+', re.I)
word_counter = Counter(re.findall(word_pattern, text))
print([word for word, counter in word_counter.items() if counter > 1])
# ['symbols', 's']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句