pythonregexregex-lookaroundsregex-negation

Capturing negative lookahead


I need for https://github.com/mchelem/terminator-editor-plugin to capture different type of path with line number. So far I use this pattern:

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

I'm trying to make it work for git patch format, which adds a 'a/' and 'b/' before paths, how do I make this work, I can't make the lookahead gulp the first slash. Here's the test text:

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

(In this link I want the same capture text except for the first line where it is currrently capturing /src/give/forms.py and /src/give/forms.py but I want src/give/forms.py and src/give/forms.py)


Solution

  • You have redundant groups that you can remove and make the starting [ab]/ part optional to leave it out of first capture group.

    Here is refactored and optimized regex:

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

    Updated RegEx Demo