Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Regex查找位置附近有多个相同字符串出现的单词_Python_Regex - Fatal编程技术网

Python Regex查找位置附近有多个相同字符串出现的单词

Python Regex查找位置附近有多个相同字符串出现的单词,python,regex,Python,Regex,我正在尝试使用正则表达式查找如下单词。但是,我找不到区分字母表和相同字母表的方法 例如: text = ' I am sooo hungryyyy....Grrrh ...... helppp meeeeee ' pattern = re.compile(r"(.)\1{1,}", re.DOTALL) 这种模式没有多大帮助。不知道为什么。 我想要一个正则表达式来匹配所有的单词,比如sooo,hungryyyyy,Grrrh…。这意味着,如果一个字母同时或相邻重复至少2次。如果要将非空白字符与

我正在尝试使用正则表达式查找如下单词。但是,我找不到区分字母表和相同字母表的方法

例如:

text = ' I am sooo hungryyyy....Grrrh ...... helppp meeeeee '
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
这种模式没有多大帮助。不知道为什么。
我想要一个正则表达式来匹配所有的单词,比如
sooo
hungryyyyy
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']
给出:


现在有什么问题
re.findall(模式,文本)
给出
['o','y','r','p','e']
@thefourtheye-但我想要一个接一个的完整单词。你必须使用正则表达式还是可以接受另一种解决方案?这意味着,如果一个字母在重复,这里如何将
视为字母?@thefourtheye-欢迎另一种解决方案。
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')