What are repeating character classes used in Python regular expression?

PythonServer Side ProgrammingProgramming

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.

raja
Published on 09-Jan-2018 18:40:50
Advertisements