firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.26k stars 199 forks source link

Python - wrongly reported (in)correctness of flag modifiers #2070

Open pszlazak opened 1 year ago

pszlazak commented 1 year ago

Bug Description

If I test usage of inline flag modifiers for Python flavor, results are wrongly reported.

Disabling flags using inline modifiers

Reproduction steps

Regex101 accepts following pattern: https://regex101.com/r/QT1SaN/1

Expected Outcome

Pattern should not be accepted, as Python does not accept disabling flags:

Python 3.6.8 (default, Sep 26 2019, 11:57:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.search(r'(?i-m)test', 'TeSt').group()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.6/re.py", line 182, in search
    return _compile(pattern, flags).search(string)
  File "/usr/lib64/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/lib64/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/lib64/python3.6/sre_parse.py", line 855, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/lib64/python3.6/sre_parse.py", line 416, in _parse_sub
    not nested and not items))
  File "/usr/lib64/python3.6/sre_parse.py", line 734, in _parse
    flags = _parse_flags(source, state, char)
  File "/usr/lib64/python3.6/sre_parse.py", line 820, in _parse_flags
    raise source.error(msg, len(char))
sre_constants.error: missing : at position 5

Tested also with Python 3.7 and 3.11.

But generally it is possible to enable flags using inline modifier:

Python 3.6.8 (default, Sep 26 2019, 11:57:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.search(r'(?i)test', 'TeSt').group()
'TeSt'

Disabling flags using localized inline modifiers

Reproduction steps

Regex101 does not accept following pattern: https://regex101.com/r/QT1SaN/2.
'Pattern error' is reported.

Expected Outcome

Such pattern should be accepted. But this feature was added in Python3.6 (released in 2016):

Python 3.6.8 (default, Sep 26 2019, 11:57:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.search(r'(?i-m:test)', 'TeSt').group()
'TeSt'

Browser

Chrome 113 Firefox 112

OS

Windows 10