Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 - Fatal编程技术网

在python中查找正确的正则表达式以匹配模式并提取子字符串

在python中查找正确的正则表达式以匹配模式并提取子字符串,python,regex,Python,Regex,我有一个文本可以如下所示: 36] Smarandache F. (Editor), Proceedings of the First International Conference on Neutrosophics, Univ. of New Mexico, Gallup Campus, NM, USA, 1-3 Dec. 2001, Xiquan, Phoenix, 2002 我想摘录: Proceedings of the First International Conference

我有一个文本可以如下所示:

36] Smarandache F. (Editor), Proceedings of the First International Conference on Neutrosophics, Univ. of New Mexico, Gallup Campus, NM, USA, 1-3 Dec. 2001, Xiquan, Phoenix, 2002
我想摘录:

Proceedings of the First International Conference on Neutrosophics
我尝试使用regex模式,如下所示:

conference = re.search(",(.*)conference(.*),", str(r.lower()))
我只得到这个作为输出: 第一国际会议记录

我的文本将是随机的,但它将包含像conference这样的单词

我的问题是如何开发一种模式,可以在文本中找到单词conference,并将子字符串从单词conference之前的第一个逗号提取到单词conference之后的第一个逗号

,XXXXXXXXXXXXXXXX会议XXXXXXXXXXXXXXXXX


任何帮助都会很好

您可以使用一个否定字符类来匹配除逗号以外的任何字符,并在匹配会议之间与单个捕获组进行匹配

您可以匹配以大写字母
C
开头的会议以获得结果,或者使用
re.IGNORECASE

如果使用
r.lower()
将字符串转换为小写,输出结果将是:

第一届中性粒细胞学国际会议记录


示例代码:

import re
r = "36] Smarandache F. (Editor), Proceedings of the First International Conference on Neutrosophics, Univ. of New Mexico, Gallup Campus, NM, USA, 1-3 Dec. 2001, Xiquan, Phoenix, 2002"

conference = re.search(r",\s*([^,]*\bConference\b[^,]*),", r)
if conference:
    print(conference.group(1))
输出

Proceedings of the First International Conference on Neutrosophics

您可以使用一个否定字符类来匹配除逗号以外的任何字符,并在匹配之间与单个捕获组进行匹配

您可以匹配以大写字母
C
开头的会议以获得结果,或者使用
re.IGNORECASE

如果使用
r.lower()
将字符串转换为小写,输出结果将是:

第一届中性粒细胞学国际会议记录


示例代码:

import re
r = "36] Smarandache F. (Editor), Proceedings of the First International Conference on Neutrosophics, Univ. of New Mexico, Gallup Campus, NM, USA, 1-3 Dec. 2001, Xiquan, Phoenix, 2002"

conference = re.search(r",\s*([^,]*\bConference\b[^,]*),", r)
if conference:
    print(conference.group(1))
输出

Proceedings of the First International Conference on Neutrosophics

中间结果包含逗号吗?你正在匹配它。见右图,第1组,第2组,全场比赛。如果您想提取特定的内容或使用未命名的组,请使用名称捕获组。我在没有“会议”一词的组中获得结果。这就像在word conference上拆分它,并得到结果,直到行尾。这不是我想要的。将我链接的正则表达式从
,(*)conference(.*),
更改为
,(.*conference.*),
,您将看到一个完全符合您要求的组。你可能想做<代码>,(.*.会议?*),< /代码>有非贪婪/懒惰的匹配。好的,让我试试Tindid,结果中间包含逗号。你正在匹配它。见右图,第1组,第2组,全场比赛。如果您想提取特定的内容或使用未命名的组,请使用名称捕获组。我在没有“会议”一词的组中获得结果。这就像在word conference上拆分它,并得到结果,直到行尾。这不是我想要的。将我链接的正则表达式从
,(*)conference(.*),
更改为
,(.*conference.*),
,您将看到一个完全符合您要求的组。您可能需要执行
,(.*?conference.*),
以进行非贪婪/惰性匹配。好的,让我尝试一下TinThanks,以获得答复我看到它在正则表达式演示中运行,但在python交互终端中没有empty@JaskaranSingh我添加了示例代码,您必须从组1获得值。感谢您的回复,我看到它在regex演示中运行,但在我的python交互终端中没有empty@JaskaranSingh我添加了示例代码,您必须从组1中获得值。