Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negate regex Notepad++

I know, it seems like yet another duplicate question, but I can't make it work.

My pattern :\d\s" works fine, but when I'm trying to add negation to find lines that do not contain the above regex, n++ doesn't find any. I've tried (?!:\d\s"), with different variations of wild cards, to no avail.

Sample data:

blah_blah:0 "More blah..."

blah_blah:0 More blah..."

(space at the beginning and \n at the end of the lines).

I want to be able to catch missing quote for example (see the second line).

UPD: I need to invert my regex to find incorrectly formatted lines.

like image 747
Al Crow Avatar asked Mar 16 '26 07:03

Al Crow


2 Answers

Use your :\d\s" regex in Mark tab (just check the Bookmark line option) and after clicking Mark All, click Search -> Bookmark -> Inverse Bookmark.

If you want a regex way of solving this task, you need to use

^(?!.*:\d\s").+

This regex will highlight any non-empty line has no match for the :\d\s" pattern.

Details:

  • ^ - start of a line
  • (?!.*:\d\s") - that has no :, digit, whitespace and " after any 0+ chars other than line break chars
  • .+ - any 1 or more chars other than line break chars

enter image description here

like image 160
Wiktor Stribiżew Avatar answered Mar 18 '26 01:03

Wiktor Stribiżew


You could use this regular expression:

(?:^|\r?\n)(?!.*?:\d\s.*?).*?(?=\r?\n|$)

It will match everything between line endings (or start/end of document), which does not contain :\d\s in it.

  • (?:^|\r?\n) matches the document start or a line start (UNIX and Windows line endings allowed) in a non-matching group (?:).
  • (?!.*?:\d\s.*?).*? matches everything except .*?\:\d\s.*?.
  • (?=\r?\n|$) marks a positive lookahead for a line-ending \r\n or a document ending $. The positive lookahead is necessary, because the line ending would otherwise already be matched by the first part of the regex. Essentially, it would skip every second line, if this part would be matching too (overlapping is not possible in regular regex).

Here is an example in JavaScript:

var regex = /(?:^|\r?\n)(?!.*?:\d\s.*?).*?(?=\r?\n|$)/g;
var text = document.getElementById("main").innerHTML; // read the line of the HTML snippet (easier to write lines in HTML);
var match = text.match(regex);
console.log(match);
<div id="main">
  line:1 abc123abc123
  line: abc123abc123
  line:3 abc123abc123
  line:4abc123abc123
  line:5 abc123abc123
  line6 abc123abc123
  lineabc123abc123
</div>
like image 27
ssc-hrep3 Avatar answered Mar 18 '26 01:03

ssc-hrep3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!