3

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

Tanish Sarmah
  • 430
  • 5
  • 14

1 Answers1

7

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.

Kraay89
  • 919
  • 1
  • 7
  • 19
  • You're welcome. Please accept the answer with the green checkmark near (under?) the voting buttons! Good luck with the rest of your learning trajectory. Regexes can be quite hard to get a grasp off, but very powerfull once you understand them! – Kraay89 Aug 27 '20 at 11:32