下面我有几个示例字符串,使用正则表达式,我必须得到它的一部分,直到最后一次出现开闭括号,开闭括号中有可选值.

START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end ()END

START OF THE SHOW with some gibberish characters () also lots of non-sense things () with boring end ()END

START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end (III)END

START OF THE SHOW with some gibberish characters (1) also lots of non-sense things (2) with boring end (3)END

START OF THE SHOW with some gibberish characters (111) also lots of non-sense things (222) with boring end (333)END

我正在寻找的结果:

START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end 

START OF THE SHOW with some gibberish characters () also lots of non-sense things () with boring end 

START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end 

START OF THE SHOW with some gibberish characters (1) also lots of non-sense things (2) with boring end 

START OF THE SHOW with some gibberish characters (111) also lots of non-sense things (222) with boring end 

首先,我试着像这样使用non-capture group的概念,但我只得到匹配,直到开闭括号的第一个匹配.regexnon-capture group

^(.*?)(?:\(.*\))END$

但我从所有字符串中得到的输出只有这么多:

START OF THE SHOW with some gibberish characters 

然后我试着用negative lookaheadnon-capture groupregexnegative lookahead

^(.*?)(?:\(.*\))END$

但我得到了匹配,包括最后出现的开闭括号.

START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end ()

START OF THE SHOW with some gibberish characters () also lots of non-sense things () with boring end ()

START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end (III)

START OF THE SHOW with some gibberish characters (1) also lots of non-sense things (2) with boring end (3)

START OF THE SHOW with some gibberish characters (111) also lots of non-sense things (222) with boring end (333)

我很确定我错过了一些小东西,但我不能自己弄清楚.

我很高兴有人能指出这一点.

推荐答案

你可以使用积极的前瞻来做到这一点:

^.*(?= \(.*\)END$)

这将捕获后面跟着([arbitrary characters])END的任何内容

https://regex101.com/r/II2nu3/1

如果END是你为了更好地解释你的问题而添加的东西,而你只是想要最后一对()之前的所有东西,^.*(?= \(.*\)$)就可以了

Python-3.x相关问答推荐

模型中的__str__方法在Django管理面板中生成大量重复查询

如果行在所有上级索引中都为0,如何删除下级索引行?

Django内置注销视图`不允许的方法(GET):/USERS/LOGOUT/`

谁能解释一下这个带邮编的多功能环路?

从.csv导入将文件夹路径加入到文件名

对大型数据框中的选定列进行重新排序

如果集合大于 len(x),则 pandas 在重复的行中拆分集合列

嵌套协议的使用(协议成员也是协议)

聚合(aggregate)为最多包含两个元素的列表

具有函数值的 Python 3 枚举

参数化泛型不能与类或实例判断一起使用

python 3中的SQLAlchemy ER图

保存 StandardScaler() 模型以用于新数据集

两个字符串之间的正则表达式匹配?

Python 3 list(dictionary.keys()) 引发错误.我究竟做错了什么?

PySpark python 问题:Py4JJavaError: An error occurred while calling o48.showString

在python中,如果一个函数没有return语句,它会返回什么?

Python的max函数有多高效

Python 3 - Zip 是 pandas 数据框中的迭代器

Pylint 中的模块PyQt5.QtWidgets错误中没有名称QApplication