Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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:选择相邻的两个(hashtag)单词的所有组_Python_Regex - Fatal编程技术网

Python Regex:选择相邻的两个(hashtag)单词的所有组

Python Regex:选择相邻的两个(hashtag)单词的所有组,python,regex,Python,Regex,我有一个示例字符串: #water #atlantic ocean #sea 我想用正则表达式来选择两组相邻的标签词。这将返回: [[['#water']['#atlantic ocean']], [['#atlantic ocean']['#sea']]] 我不知道怎么做这个正则表达式。我得到的最接近的结果是: ([#][A-Za-z\s]+\s?) 这只会产生以下结果(在python中): 我试着在末尾加一个{2},但这似乎不匹配。关于如何实现这一点,您有什么想法吗?为了进行重叠匹配,您

我有一个示例字符串:

#water #atlantic ocean #sea
我想用正则表达式来选择两组相邻的标签词。这将返回:

[[['#water']['#atlantic ocean']], [['#atlantic ocean']['#sea']]]
我不知道怎么做这个正则表达式。我得到的最接近的结果是: ([#][A-Za-z\s]+\s?)

这只会产生以下结果(在python中):

我试着在末尾加一个{2},但这似乎不匹配。关于如何实现这一点,您有什么想法吗?

为了进行重叠匹配,您需要使用

(?=(#[A-Za-z]+(?:\s[A-Za-z]+)?\s#[A-Za-z]+(?:\s[A-Za-z]+)?))

如果下列单词出现零次或多次,请在非捕获组后使用
*
而不是

>>> m = re.findall(r'(?=(#[A-Za-z]+(?:\s[A-Za-z]+)*)\s(#[A-Za-z]+(?:\s[A-Za-z]+)*))', s)
>>> print m
[('#water', '#atlantic ocean'), ('#atlantic ocean', '#sea')]
试试这个。这将提供所需的组。抓取捕获

x="#water #atlantic ocean #sea"
print re.findall(r"(#[^#]*)(?=[^#]*(#[^#]*))",x)
输出:
[(“#水”、“大西洋”)、(“#大西洋”、“海洋”)]

见演示


对我来说,在
#
上拆分比使用复杂的正则表达式更直观:

import re
expr = "#water #atlantic ocean #sea"
groups = filter(None, re.split(r' ?#', expr))
# another option is to use a split that doesn't require regex at all:
# groups = filter(None, map(str.strip, expr.split("#"))) 
res = []
for i, itm in enumerate(groups):
    if i < len(groups)-1:
        res.append(["#"+itm, "#"+groups[i + 1]])

print res  # [['#water', '#atlantic ocean'], ['#atlantic ocean', '#sea']]
重新导入
expr=“#水#大西洋#海”
组=过滤器(无,重新拆分(r'?#',expr))
#另一种选择是使用完全不需要正则表达式的拆分:
#groups=filter(无,映射(str.strip,expr.split(“#”))
res=[]
对于i,枚举(组)中的itm:
如果i
如果有四个标签怎么办?两个(标签)单词“相邻”是什么意思?例如,在我看来,大西洋和海洋并不相邻。它们之间用非标签词
ocean
隔开。您得到了
#atlantic
,但OP想要
#atlantic
呃。。您的代码和演示不匹配。你的代码实际上没有给出海洋。当我尝试它时,它确实起作用了。。。x=“#水#大西洋#海”印刷品关于findall(r)(#[^#]*)(?=[^#]*(#[^#]*)”,x)[(#水',#大西洋',(#大西洋',#海]]正如我前面提到的,它也适用于扩展。这一个实际上最适用于我正在尝试的操作,因为它确实返回元组,我不必进行额外的拆分,这就是我选择它的原因。如果hashtag还包括@symbols或其他类型呢?你能在多个字符上拆分吗?我想这最适合我要做的事情,因为如果我需要的话,我可以将它扩展到多个选项,比如@s。。。非常感谢。
>>> m = re.findall(r'(?=(#[A-Za-z]+(?:\s[A-Za-z]+)*)\s(#[A-Za-z]+(?:\s[A-Za-z]+)*))', s)
>>> print m
[('#water', '#atlantic ocean'), ('#atlantic ocean', '#sea')]
(#[^#]*)(?=[^#]*(#[^#]*))
x="#water #atlantic ocean #sea"
print re.findall(r"(#[^#]*)(?=[^#]*(#[^#]*))",x)
import re
expr = "#water #atlantic ocean #sea"
groups = filter(None, re.split(r' ?#', expr))
# another option is to use a split that doesn't require regex at all:
# groups = filter(None, map(str.strip, expr.split("#"))) 
res = []
for i, itm in enumerate(groups):
    if i < len(groups)-1:
        res.append(["#"+itm, "#"+groups[i + 1]])

print res  # [['#water', '#atlantic ocean'], ['#atlantic ocean', '#sea']]