What are repeating character classes used in Python regular expression?


A character class followed by operators like '?', '*' or '+' are called repeating character classes.

If you repeat a character class by using the '?', '*' or '+' operators, you will repeat the entire character class, and not just the character that it matched. The regex '[0-9]+' can match '579' as well as '333'. If you want to repeat the matched character, rather than the class, you will need to use backreferences. '([0- 9])\1+' will match '333' but not “579”. When applied to the string “922226”, it will match '2222' in the middle of this string. If you do not want that, you need to use lookahead and lookbehind.

Updated on: 30-Jul-2019

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements