0
votes

I'm getting an error message when I run my code... (Python 3.5.1)

sre_constants.error: nothing to repeat at position 0

I can't find mistakes in my code, I think the error isn't even in my code.

This is my code:

import re

numberRegexa = re.compile(r'06\d{8}')
numberRegexb = re.compile(r'06 \d{8}')
numberRegexc = re.compile(r'31 6 \d{8}')
numberRegexd = re.compile(r'+31 6 \d{8}')
numberRegexe = re.compile(r'316\d{8}')
numberRegexf = re.compile(r'+316\d{8}')
numberRegexg = re.compile(r'316 \d{8}')
numberRegexh = re.compile(r'+316 \d{8}')
numberRegexi = re.compile(r'31 6\d{8}')
numberRegexj = re.compile(r'+31 6\d{8}')

##numberRegexAll = [
##    (1,  numberRegex06a),
##    (2,  numberRegex06b),
##    (3,  numberRegex31a),
##    (4,  numberRegex31b),
##    (5,  numberRegex31c),
##    (6,  numberRegex31d),
##    (7,  numberRegex31e),
##    (8,  numberRegex31f),
##    (9,  numberRegex31g),
##    (10, numberRegex31h),
##]

def searchNumber():
    result = numberRegex06a.search(userInput)
    print('Found: ' + result.group())

userInput = input("Insert text you'd like searched:\n")
searchNumber()

And this is my log:

Traceback (most recent call last): File "C:/Users/Hibuna/Desktop/asdsad.py", line 6, in numberRegexd = re.compile(r'+31 6 \d{8}')

File "C:\Programs\Python 3.5.1\lib\re.py", line 224, in compile return _compile(pattern, flags)

File "C:\Programs\Python 3.5.1\lib\re.py", line 293, in _compile p = sre_compile.compile(pattern, flags)

File "C:\Programs\Python 3.5.1\lib\sre_compile.py", line 536, in compile p = sre_parse.parse(p, flags)

File "C:\Programs\Python 3.5.1\lib\sre_parse.py", line 829, in parse p = _parse_sub(source, pattern, 0)

File "C:\Programs\Python 3.5.1\lib\sre_parse.py", line 437, in _parse_sub itemsappend(_parse(source, state))

File "C:\Programs\Python 3.5.1\lib\sre_parse.py", line 638, in _parse source.tell() - here + len(this))

What the hell is going on? I really don't understand this advanced stuff, I'm pretty new to programming..

Thanks in advance!

1
"I think the error isn't even in my code." - get used to the problem being in your code. It almost always is. Did you look at the line in your code that the traceback is pointing to? There's a problem in your regex syntax. - user2357112 supports Monica
Hint: a + sign is special; you'll want to escape it. - user707650
@Poeder the most interesting line is the first one of the traceback. It shows you, where the error is in your file. - bastelflp

1 Answers

1
votes

You can test your regex patterns here: https://regex101.com/r/eY4aT8/1. They are explained in detail and errors are depicted.

The problem is your regex pattern +31 6 \d{8} - it cannot just start with a + with nothing on the left of that +.