Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match text that contains n or more of specified character

Tags:

regex

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
like image 834
Damian Avatar asked Jan 22 '13 17:01

Damian


People also ask

What is the regular expression matching one or more specific characters?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus .

How do I match a specific character in regex?

Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.

What does ?= Mean in regular expression?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

What is the regular expression matching zero or more specification characters?

3. What is the Regular Expression Matching Zero or More Specific Characters? Explanation: Zero or Specific Expression matching can be done only by a single character that is*.


1 Answers

.* 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.

like image 98
Tim Pietzcker Avatar answered Oct 29 '22 14:10

Tim Pietzcker