Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matches for string not preceeded by certain word

In sourcetree there's an option to use regex to make links.

I'm using #number and #number for both the task number and the pull request numbers.

'Usually' the pull request number is preceded by the string "pull request" or "Pull Request" or "Pull request".

At the moment I'm using: #(\d+) to match the task number but obviously this also matches the pull request number so I think that to accomplish what I want I need to only match #(\d+) if it is not following the words "pull request" how can I do this? or is there a better way to do what I'm trying to do?

like image 596
Aequitas Avatar asked Dec 06 '25 02:12

Aequitas


1 Answers

Use a negative look behind:

(?<![pP]ull [rR]equest)#number

The syntax (?<!...) is a negative look behind. It asserts, but does not capture, that the characters preceding it do not match the pattern ....

There are three other flavours or look arounds:

  • (?<=...) a (positive) look behind
  • (?=...) a (positive) look ahead
  • (?!...) a negative look ahead

With similar, and hopefully obvious, effect.

like image 128
Bohemian Avatar answered Dec 08 '25 15:12

Bohemian