[英]Regex to find words having multiple occurrences of same string nearby position
我正在嘗試使用正則表達式查找類似以下的單詞。 但是,我找不到將字母與相同字母區分開的想法。
例如 :
text = ' I am sooo hungryyyy....Grrrh ...... helppp meeeeee '
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
這種模式不是那么有用。 不知道為什么。 我想要一個正則表達式匹配所有單詞,例如sooo
, hungryyyy
, Grrrh
....
這意味着,如果一個字母同時重復或相鄰重復至少2次。
如果您想將非空白與連續字符進行匹配,可以執行以下操作:
>>> import re
>>> text = 'I am sooo hungryyyy....Grrrh ...... helppp meeeeee'
>>> matches = re.findall(r'(\S*?(.)\2+\S*?)', text)
>>> [x[0] for x in matches]
['sooo', 'hungryyyy', '....', 'Grrr', '......', 'helppp', 'meeeeee']
這意味着,如果一個字母同時重復或彼此重復至少2次...
但是,如果您要查找文字字符,則您的模式將簡單地更改:
>>> matches = re.findall(r'(\w*(\w)\2\w*)', text)
>>> [x[0] for x in matches]
['sooo', 'hungryyyy', 'Grrrh', 'helppp', 'meeeeee']
import re
text = ' I am sooo hungryyyy....Grrrh ...... helppp meeeeee '
for p in re.findall(r'(\w*(\w)\2\w*)', text):
print p
得到:
('sooo', 'o')
('hungryyyy', 'y')
('Grrrh', 'r')
('helppp', 'p')
('meeeeee', 'e')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.