正则表达式匹配第n次出现并返回字符串的后续部分

时间:2019-05-25 15:51:06

标签: regex

我正在使用Zapier提取模式,并且需要使用Regex从以下数据中为Zap的每个步骤返回不同的名称:

“与样本名称A(https://www进行的会话。

“来自样品名称A的消息”

与样本名称B(https://www的会话。

“来自样品名称B的消息”

与样本名称C(https://www的会话。

“来自样品名称C的消息”

样本名称为D(https://www的会话。

“来自样品名称D的消息”'

例如,对于第三次击打,我想返回位于“与对话”和“(https://www。”的第三个实例之间的所有内容,在这种情况下,该实例为Sample Name C

每次运行Regex时,名称都会不同。

到目前为止,我有^.*Conversation with (\S[^(]*)(?: |\z),它返回样品名称A。

如何调整代码以返回列表中的第二,第三或第四名称?

谢谢!

2 个答案:

答案 0 :(得分:1)

你会这样的。
要获得第n个,只需将其放入下面的from setuptools import setup with open('README.md') as f: long_description = f.read() setup( name="gutenberg_cleaner", install_requires=['nltk'], version='0.1.0', description="cleans gutenberg dataset books", author_email='mohsenikiasari@ce.sharif.edu', py_modules=["gutenberg_cleaner"], url="https://github.com/kiasar/gutenberg_cleaner", license='MIT', long_description=long_description, classifiers=[ "Programming Language :: Python", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Operating System :: OS Independent", ], ) 量词中即可。
在这种情况下,我们得到第三个。

{nth}

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

解释

(?mis)\A(?:.*?^Conversation[ ]+with[ ]+){3}(.*?)(?=[ ]*\(https://)

答案 1 :(得分:0)

如果我理解正确,我们只想提取样本名称,我们可以从右边界为(的表达式开始尝试,并且左边界已在您的原始表达式中定义:

(?:Conversation with )(.+?)(?:\s+\(.+)

DEMO

enter image description here

相关问题