python - 匹配一系列连字符后的所有内容

标签 python regex

我正在 try catch 文件中行首三个连字符之后的所有剩余文本 (---)。

示例:

Anything above this first set of hyphens should not be captured.

---

This is content. It should be captured.
Any sets of three hyphens beyond this point should be ignored.

应捕获第一组三个连字符之后的所有内容。我得到的最接近的是使用这个正则表达式 [^(---)]+$ ,它的效果稍微好一些。它将捕获连字符之后的所有内容,但如果用户在该点之后放置任何连字符,它将捕获用户放置的最后一个连字符之后的内容。

我将其与 python 结合使用来捕获文本。

如果有人能帮我解决这个正则表达式问题,我将不胜感激。

最佳答案

pat = re.compile(r'(?ms)^---(.*)\Z')

(?ms) 添加了 MULTILINEDOTALL 标志。

MULTILINE 标志使 ^ 匹配行的开头(而不仅仅是字符串的开头)。我们需要这个,因为 --- 出现在行的开头,但不一定是字符串的开头。

DOTALL 标志使 . 匹配任何字符,包括换行符。我们需要这个,以便 (.*) 可以匹配多行。

\Z 匹配字符串的结尾(而不是行的结尾)。

例如,

import re

text = '''\    
Anything above this first set of hyphens should not be captured.

---

This is content. It should be captured.
Any sets of three hyphens beyond this point should be ignored.
'''

pat = re.compile(r'(?ms)^---(.*)\Z')
print(re.search(pat, text).group(1))

打印

This is content. It should be captured.
Any sets of three hyphens beyond this point should be ignored.
<小时/>

请注意,当您使用括号 [...] 定义正则表达式字符类时,括号内的内容是(一般情况下,除了像 a-z 这样的连字符范围) >) 解释为单个字符。它们不是模式。因此 [---][-] 没有什么不同。事实上,[---] 是从 -- 的字符范围(包含在内)。

字符类中的括号也被解释为文字括号,而不是分组分隔符。所以[(---)]相当于[-()],字符类包括连字符和左右括号。

因此,字符类 [^(---)]+ 匹配除连字符或括号之外的任何字符:

In [23]: re.search('[^(---)]+', 'foo - bar').group()
Out[23]: 'foo '

In [24]: re.search('[^(---)]+', 'foo ( bar').group()
Out[24]: 'foo '

您可以看到这是怎么回事,以及为什么它不能解决您的问题。

关于python - 匹配一系列连字符后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413879/

相关文章:

java - 从包含特殊字符的字符串中提取子字符串

php - preg_split 字符串不在单引号之间

python - 套接字无法建立连接

python - 安装两个同名的python模块

python - 使用python获取维基百科摘要

python - 在 python 中,是否有最佳实践方法来清理包含需要转义字符的正则表达式搜索字符串?

php - 使用正则表达式提取两个短语之间的所有单词

python - 获得 kolmogorov-smirnov 检验所需的临界值

python - 将 width x height x 3 numpy uint8 数组合并为 width x height x 1 uint32 数组

c# - 使用正则表达式匹配字符串,以特定字符串开头和结尾且中间不包含字符串