Search code examples
regexnsregularexpression

How to write a regex that matches duplicated words with 5 occurrences?


I want to match the duplicated words which in this instance is GHL_AutoMatchFaulted In this example, there are 5 occurrences which I want to match all 5 otherwise it will not match

GHL_AutoMatchFaulted
GHL_AutoMatchFaulted
GHL_AutoMatchFaulted
GHL_AutoMatchFaulted
GHL_AutoMatchFaulted

Is this possible?

Any help would be much appreciated. Thanks.

Tried almost all from the forum but nothing works.


Solution

  • How about this one: ?

    (GHL_AutoMatchFaulted\s?\n?){5}
    
    • Looks for the keyword GHL_AutoMatchFaulted
    • with an optional space \s?
    • and an optional carriage return \n?
    • for exactly 5 times {5}

    As per Regex101:

    • it will match 5 replicates in a row,
    • but not 4 replicates in a row.
    • however it will match the first 5 replicates when there are 6 replicates in a row.

    Regex101