捕捉负面前瞻

问题描述 投票:0回答:1

我需要 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.py
src/give/forms.py

python regex regex-lookarounds regex-negation
1个回答
0
投票

您有多余的组,可以将其删除,并将起始

[ab]/
部分设为可选,以将其排除在第一个捕获组之外。

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

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

更新了正则表达式演示

© www.soinside.com 2019 - 2024. All rights reserved.