Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 带逗号的正则表达式搜索_Python_Regex_Search - Fatal编程技术网

Python 带逗号的正则表达式搜索

Python 带逗号的正则表达式搜索,python,regex,search,Python,Regex,Search,需要一些关于正则表达式的帮助 str = 'label1 a1,832,b2 and label2 2, c45' 正在尝试将结果返回为 ['label a1',label 832','label b2','label 2', 'label c45'] 到目前为止,我只能获得['label2 a1','label2 2'] 谢谢 编辑: 请澄清 我有一份标签清单 labelList = ['dog','cat','mouse',...] str = 'There are 3 locatio

需要一些关于正则表达式的帮助

str = 'label1 a1,832,b2 and label2 2, c45'
正在尝试将结果返回为

['label a1',label 832','label b2','label 2', 'label c45']
到目前为止,我只能获得
['label2 a1','label2 2']

谢谢

编辑:

请澄清

我有一份标签清单

labelList = ['dog','cat','mouse',...]

str = 'There are 3 locations which are dog 122, h25 and cat a3.'
结果应该是:

result = 'dog 122', 'dog h25' and 'cat a3'.
目前,我正在进行正则表达式搜索:

for x in labelList:
    re.search(r'\b(%s) ([^ \r\n]+\b')
希望这能澄清问题

编辑2:

labelList = ['dog','cat','mouse',...]

str = 'There are 3 locations which are dog 122, h25 and cat a3.'

for x in labelList:

    if re.search(r'\b(%s)\b' % (x), str):

        nr = [f"(%s) {m}" % (x) for m in re.findall(r"(?:(%s))?(\w+)",  comText) if m!= 'and']
        print(nr)
然而,输出似乎是错误的。它给我以下输出

["(dog) (' ','there')", "(dog) (' ','are')", "(dog) (' ', '3')" ... 

好的。到目前为止,我有一种方法可以提取其中一个标签后面的文本,可以是一个句点,也可以是单词“and”

标签分两个阶段提取。首先,我们得到一个元组列表,其中第一项是物种(狗、猫),第二项是标签(或标签)的原始列表。然后进行迭代以构建最终标签

如果要编译表达式,请将编译代码放在只需执行一次的位置,否则编译的意义何在

或者使用
.finditer()
而不是
.findall()

试试这个:

import re
str = 'label1 a1,832,b2 and label2 2, c45'
str = str.replace('and', ',')
str = re.sub(r"label[0-9]+", "", str)
labels = ['label {}'.format(x.strip()) for x in str.split(',')]
输出为:

labels = ['label a1', 'label 832', 'label b2', 'label 2', 'label c45']
在我看来,标签[0-9]+和子字符串
都是无用的信息。

只需删除它并提取标签名称。然后使用
string.format()
function

重建字符串。到目前为止,我只能得到…,你试图得到的代码在哪里?使用split,如果你包含正则表达式,我们可以给你一些提示。
a1
832
2
c45
的共同特点是它们都紧跟着逗号,或者都在一行的末尾。我们可以用
\w+(?=,|$)
匹配这些。那么
b2
呢?什么规则告诉我们您也希望提取该字符串?首先,你需要用文字告诉我们匹配规则,然后举例说明。在单个示例中说明的问题很少是明确无误的,这也不例外。
“label”
是给定的,还是字符串可以是
“cat1 A1832,b2和cat2 2,c45”
,在这种情况下,您希望返回
[“cat a1”,“cat 832”,“cat b2”,“cat 2”,“cat c45”]
?谢谢您的回答!我有一个标签列表,其中labelList=[label1,label2,label3…],因此label1和label2是唯一的标签。那么,我应该如何修改代码呢?如果上面没有处理@datajemYes this is的所有情况,您可以发布更多需要解析的目标文本示例。谢谢。如果您发现任何有帮助的解决方案,请向他们投票@datajem;-)他们试图提高投票率。低于15%的声誉不会反映在公开展示的分数上。谢谢托德!使用f-strings将是更简单的语法。
import re
str = 'label1 a1,832,b2 and label2 2, c45'
str = str.replace('and', ',')
str = re.sub(r"label[0-9]+", "", str)
labels = ['label {}'.format(x.strip()) for x in str.split(',')]
labels = ['label a1', 'label 832', 'label b2', 'label 2', 'label c45']