Regarding Pyhton regular error: sre_constants.error: nothing to repeat at position

Wrong regular expression

1. \b followed by the quantity

Wrongly written \d to \b, embarrassing!

>>> pattern = re.compile(r'123\b*hello')


Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "E:\Anacoda3\Lib\", line 233, in compile
    return _compile(pattern, flags)
  File "E:\Anacoda3\Lib\", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "E:\Anacoda3\Lib\", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "E:\Anacoda3\Lib\", line 856, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
  File "E:\Anacoda3\Lib\", line 415, in _parse_sub
    itemsappend(_parse(source, state, verbose))
  File "E:\Anacoda3\Lib\", line 615, in _parse
    source.tell() - here + len(this))
sre_constants.error: nothing to repeat at position 2

Reason for error: Since \b is a word boundary, * means that it appears any number of times, that is, a word can only have one boundary, and it cannot appear any number of times, so this error will be reported

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *