Why am I getting the error-"re.error: multiple repeat at position 2"

Viewed 3300

I am learning re module for the first time but got an error .

The code-

import re
my_str='''pyhton
c++
java
c++
js node
ds algo
pyhton
js node
javac++
java
js node
ds algo'''
var = re.findall("c++",my_str)

It gives the error - re.error: multiple repeat at position 2

1 Answers

Check out the Python RE module documentation. The '+' character has a special meaning in Regex. It denotes that the previous character is repeated one or more times.

so 'c++' as a regex actually means "the character 'c' repeated one or more times repeated one or more times"

To actually identify the character '+', you need to escape it with a '\'. So your regex becomes 'c\+\+'.

I always recommend using an online regex editor to try-out your regexes in an interactive way. regexr and regex101 are good examples of such editors.

Related