python - 捕捉负面前瞻

标签 python regex regex-lookarounds regex-negation

我需要https://github.com/mchelem/terminator-editor-plugin使用行号捕获不同类型的路径。到目前为止我使用这种模式:

(?![ab]\/)(([^\t\n\r\f\v:\"])+?\.(html|py|css|js|txt|xml |json|vue))(\".line |:|\n| )(([0-9]+)*)

我正在尝试使其适用于 git patch 格式,该格式在路径之前添加了“a/”和“b/”,我该如何使其工作,我无法使前瞻吞噬第一个斜杠。这是测试文本:

diff --git a/src/give/forms.py b/src/give/forms.py


 M give/locale/fr/LC_MESSAGES/django.po
 M agive/models/translation.py
 M give/views.py

Some problem at src/give/widgets.py:103

Traceback (most recent call last):
  File "/usr/lib/python3.10/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.10/unittest/case.py", line 587, in run
    self._callSetUp()
  File "/usr/lib/python3.10/unittest/case.py", line 546, in _callSetUp
    self.setUp()
  File "/home/projects/src/give/tests/test_models.py", line 14, in setUp

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

(在此链接中,我想要相同的捕获文本,除了当前捕获 /src/give/forms.py/src/give/forms.py 的第一行 但我想要 src/give/forms.pysrc/give/forms.py)

最佳答案

您有多余的组,可以将其删除,并将起始 [ab]/ 部分设置为可选,以将其排除在第一个捕获组之外。

这是重构和优化的正则表达式:

(?:[ab]/)?([^ \t\n\r\f\v:"]+\.(?:html|py|css|js|txt|xml|json|vue))(?:". line |[:\n ])

Updated RegEx Demo

关于python - 捕捉负面前瞻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76970781/

相关文章:

正则表达式查找匹配文件扩展名的文件,除非文件名包含字符串

Python: "' str'不支持subprocess.communicate上的缓冲区接口(interface)(迁移到3.x)

python - "De-instrument"来自 sqlalchemy ORM 的实例化对象

python - 对数组的各个列进行排序

javascript - 无法转义 "?"但 "\"在 javascript 正则表达式中工作正常

c# - 正则表达式匹配 C# 中的单独整数列表

javascript - Python 中的 Webdriver - 执行 JavaScript 文件并将参数传递给其函数

python - 如何将 re.split 中的分隔符保留在返回列表的同一索引上

r - 在 R 中使用负向后查找

javascript - 在 JavaScript 正则表达式中使用前瞻匹配可选组