Reputation: 3050
I need to find lines that contains more than 10 of "," (I have got errors importing CSV, so I need to correct it manually). I'm using Notepad++ so I not need to write reqex to match line, only to match comas.
(.*,.*){11,100} //does not work
Upvotes: 18
Views: 19116
Reputation: 13631
Assuming Notepad++ version 6+ (which uses the PCRE Perl-Compatible Regular Expressions library) and that the '. matches newline' box is not ticked in the Find window:
(.*?,){11,}
If a line contains more than 10 commas, this will match from the beginning of the line to the last comma.
(.*?,)
matches any character apart from a newline as few times as possible until the next character is a comma; {11,}
means 11 or more times.
If you want the regex to work regardless of whether the '. matches newline' box is ticked, you could use:
([^\n]*?,){11,}
Your regex does work if the '. matches newline' box is not ticked, but as it matches any character greedily, there may be such an enormous number of potential matches it may appear to hang the application. Adding ?
after the .*
so that the wildcard matches lazily or reluctantly, i.e. as few times as possible, should solve the problem.
PCRE man pages
Perl Regular Expressions documentation - recommended.
Notepad++ "outdated" regular expressions tutorial
Upvotes: 6
Reputation: 336178
.*
also matches commas. You need to exclude those with a negated character class ([^,]
matches any character except commas):
^[^,\r\n]*(?:,[^,\r\n]*){11,}$
I've added \r\n
to the character class or it will match across newlines.
Be aware, though, that this will also count commas that are contained within quoted strings, so if you have those, you will misjudge the number of fields in your CSV row.
Upvotes: 21