64

I try to verify a CSV file where we had problems with line breaks.

I want to find all lines not starting with a ".

I am trying with /!^"/gim but the ! negation is not working.

How can I negate /^"/gim properly?

3 Answers 3

88

In regex, the ! does not mean negation; instead, you want to negate a character set with [^"]. The brackets, [], denote a character set and if it starts with a ^ that means "not this character set".

So, if you wanted to match things that are not double-quotes, you would use [^"]; if you don't want to match any quotes, you could use [^"'], etc.

With Notepad++, you should be able to search with the following to find lines that don't start with the " character:

^[^"]

If you want to highlight the full line, use:

^[^"].*
4
  • 4
    @newfurniturey I think the confusing part is that the chevron (hat / caret) symbol has two meanings depending on context. Either 'starts with' or 'negation', or did I misunderstand?
    – Davos
    Oct 23, 2015 at 3:42
  • What if I want it to Not have certain combination? For example I want to add aliases to fieldnames in SQL queries unless there is one set (in SQL you set an alias by simply using "table.fieldname as alias"), so I have a regex for that, but how to make sure it only matches those without an alias. (OTHERREGEX-HERE)(^([\s]+[Aa][Ss])) doesnt seem to work...
    – MahNas92
    Oct 18, 2016 at 17:14
  • 1
    @MahNas92 That sounds like it deserves its own question on the site, would be too large of an answer for a comment like this one. Oct 19, 2016 at 14:27
  • @newfurniturey maybe later on, but I can share my work-around in C# with others; I simply made a regex that fetches the last regex-pattern in a variable like this: ([\s]+[Aa][Ss])?), and then I, with programing logic, check if this last line is empty or not to decide if it really is a match...
    – MahNas92
    Oct 20, 2016 at 13:18
46

In Notepad++ you can use the very usefull negative lookahead

In your case you can try the following:

^(?!")

If you want to match wholes lines add .+ or .{1,7} or anything e.g.:

^(?!").*

will also match empty lines.

Explanation part

^ line start

(?!regexp) negative lookahead part: this means that if the regexp match, the result will not be shown

0
10

Step 1 - Match lines. Find dialog > Mark tab, you can bookmark lines that match.

Step 2 - Remove lines bookmarked OR Remove lines not bookmarked. Search > Bookmark > Remove Unmarked Lines or Remove Bookmarked lines

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.