2021年4月4日星期日

Regex Negative Lookahead Is Being Ignored

I have the following sample text

[Item 1](./path/notes.md)  [Item 2](./path)  [Item 3](./path/notes.md)  [Item 4](./path)  

When I applied the following regex \[(.*)\].*(?!notes\.md).*\), I expected the following output when I printed the first capture group

Item 2  Item 4  

but what I ended up getting was

Item 1  Item 2  Item 3  Item 4  

which, to me, seems that the negative lookahead portion, (?!notes\.md), was being ignored for some reason, so the regex was matching the whole string.

What is really confusing me, is that a positive lookahead works as I would expect. For example using \[(.*)\].*(?=notes\.md).*\) returns the following when printing the first capture group

Item 1  Item 3  

which makes sense, so I am really confused as to why the negative lookahead is not functioning properly.

https://stackoverflow.com/questions/66947059/regex-negative-lookahead-is-being-ignored April 05, 2021 at 08:38AM

没有评论:

发表评论