Search code examples
notepad++

Notepad++ -> finding records that do not match a specific pattern after the nth occurrence of a string


I am struggling trying to come up with a regex in Notepad++ to identify situations where I am looking for specific text after the nth occurrence of a character. I haven't really touched regex that much recently so I am very rusty and could use some help!

Here is an example dataset:

OK -> "Field1","Field2","Field3","Field4","Field5","Field6","Field7","Field8","Field9","Field10"

Error -> "Field1","Field2","Field3","Field4","Field5","Field6","Field7","Field8","9Field","Field10"

In this case, I want to identify records that have [^F] after the 17th occurrence of a double quote .. or the 8th occurrence of a comma. I can select the text using ^(?:.*?"){17} ... but can't figure out how to get the [^F] to fit in to the expression.

Any help would be much appreciated!

Thank you!


Solution

  • ("[^"]+",){8}"[^F]
    
    • "[^"]+", matches any quoted string followed by a coma;
    • ("[^"]+"){8}" matches exactly 8 quoted strings concatenated, each followed by a coma.

    If the matches must not span over multiple lines, add ^ at the beginning of the regex.

    Note that this solution does not work if there are spaces after the comas, or escaped quotes, but if needed it can be easily adapted.

    Edit: since you didn't mention coma, here's a solution that does not depend on comas: ^("[^"]+){16}"[^F].