Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 603
pseudo_teetotaler Avatar asked Dec 06 '22 13:12

pseudo_teetotaler


1 Answers

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.

like image 54
DenisNovac Avatar answered Dec 19 '22 16:12

DenisNovac