Can a Regex limit the instance count of any character in a string to, say, 5?
For example: abacaada would fail (or match) because of 5 instances of the character a.
To clarify, I was looking for any character, not just 'a'. I.e. no character can repeat more than x times.
Method 1: Regex re. To get all occurrences of a pattern in a given string, you can use the regular expression method re. finditer(pattern, string) . The result is an iterable of match objects—you can retrieve the indices of the match using the match. start() and match.
A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.
This regex should work:
^(?:(.)(?!(?:.*?\1){4}))*$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With