How to do negative work with end of line text

I have a regex as below:

.{0,1000}(?!(xa7|para(graf))$) 

using java. I expected this to crash the following text:

blaparagraf

because paragraf is at the end

+4
source share
2 answers

This is because .{0,1000} will match the whole item, so it does not follow xa7 or paragraf (only $ follows it).

You want a negative lookbehind:

 .{0,1000}(?<!xa7|paragraf)$ 
+6
source

It is a mistake to make mistakes in false statements. If you want to use lookahead, the template looks something like this:

 ^(?!.*paragraph$).*$ 

This matches ( as seen on rubular.com ):

 something something para paragraph something something 

But does not match:

 something paragraph 

So, the main difference is that we start looking forward at the beginning of the line before we match .* (Or .{0,1000} in your case). Of course, what we're looking for is not just paragraph$ , but rather .*paragraph$ .

However, to verify that the string does not end with something of finite length, lookbehind, when supported, is the most natural solution.

 ^.*$(?<!paragraph) 
+3
source

Source: https://habr.com/ru/post/1312381/


All Articles