python - 已知短语前后的正则表达式条件

标签 python regex

我正在 try catch 两个已知短语之间以大写字母开头的短语。让我们在“Known phrase,”和单词“The”之间说。

例如在下面的文本中,我要捕获的短语是:Stuff TO CApture That always start with Capital letter but stop capturing when

Ignore Words Known phrase, ignore random phrase Stuff TO CApture That always start with Capital letter but stop capturing when The appears.

我试过的正则表达式:(?<=Known phrase, ).*(?= The)Known phrase, (.*) The 这些正则表达式还捕获 ignore random phrase .我该如何忽略它?

最佳答案

对于您的示例数据,您可以使用:

已知短语,[a-z ]+([A-Z].*?) The

参见 regex demo

解释

  • Known phrase, 字面匹配
  • [a-z ]+ 匹配 1+ 次小写字符或空格(将允许匹配的字符类添加到大写字符除外)
  • ([A-Z].*?) 在匹配大写字符后跟除换行符之外的任何字符 0+ 次的组中捕获。
  • 逐字匹配

关于python - 已知短语前后的正则表达式条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53766816/

相关文章:

python - 将 SQL 查询转换为 ORM 查询 Django

Python/IPython 导入错误 : no module named site

正则表达式 - 如何搜索单词的单数或复数版本

c - 正则表达式 URL 捕获组

python-re.findall 如何将内容分成组

python - unicode 字符名称列表

python - self.__dict__.update(**kwargs) 风格是好还是坏?

javascript - Javascript 中两个不同字符串正则表达式之间的所有文本

Java删除所有感叹号

python - 转移多个日期列的优雅方式 - Pandas