pseudo_teetotaler
pseudo_teetotaler

Reputation: 1575

Python Regex Error : nothing to repeat at position 0

I am trying to match strings which can be typed from a normal english keyboard.

So, it should include alphabets, digits, and all symbols present on our keyboard.

Corresponding regex : "[a-zA-Z0-9\t ./,<>?;:\"'`!@#$%^&*()\[\]{}_+=|\\-]+"

I verfied this regex on regexr.com.

In python, on matching I am getting following error :

>>> a=re.match("+how to block a website in edge",pattern)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\re.py", line 163, in match
    return _compile(pattern, flags).match(string)
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\re.py", line 293, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_compile.py", line 536, in compile
    p = sre_parse.parse(p, flags)
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_parse.py", line 829, in parse
    p = _parse_sub(source, pattern, 0)
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_parse.py", line 437, in _parse_sub
    itemsappend(_parse(source, state, nested + 1))
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_parse.py", line 638, in _parse
    source.tell() - here + len(this))
sre_constants.error: nothing to repeat at position 0

Upvotes: 6

Views: 66858

Answers (2)

DenisNovac
DenisNovac

Reputation: 728

This error message is not about position of arguments. Yes, in question above they are not in the right order, but this is only half of problem.

I've got this problem once when i had something like this:

re.search('**myword', '/path/to/**myword') 

I wanted to get '**' automatically so i did not wanted to write '\' manually somewhere. For this cause there is re.escape() function. This is the right code:

re.search(re.escape('**myword'), '/path/to/**myword')

The problem here is that special character placed after the beginning of line.

Upvotes: 17

c2huc2hu
c2huc2hu

Reputation: 2497

You have your arguments for re.match backward: it should be

re.match(pattern, "+how to block a website in edge")

Upvotes: 6

Related Questions