Skip to content

fix regex flag for python 3.11 and those higher#353

Closed
emmanuel-stone wants to merge 1 commit into
WikiExtractor:masterfrom
emmanuel-stone:fix-regex-flags
Closed

fix regex flag for python 3.11 and those higher#353
emmanuel-stone wants to merge 1 commit into
WikiExtractor:masterfrom
emmanuel-stone:fix-regex-flags

Conversation

@emmanuel-stone

Copy link
Copy Markdown

Hi there, thanks for the great repo. I am an NLP researcher and I use this repo all the time for the preprocessing of wikipedia text.

For the compatibility with newer version of python, I believe this update might be an effective fix.

The issue that I had was regarding the errors such as those below.

Traceback (most recent call last):
<unknown>:3: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
<unknown>:2: SyntaxWarning: "\[" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\["? A raw string is also an option.
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/site-packages/wikiextractor/WikiExtractor.py", line 66, in <module>
    from .extract import Extractor, ignoreTag, define_template, acceptedNamespaces
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/site-packages/wikiextractor/extract.py", line 378, in <module>
    ExtLinkBracketedRegex = re.compile(
        '\[(((?i)' + '|'.join(wgUrlProtocols) + ')' + EXT_LINK_URL_CLASS + r'+)\s*([^\]\x00-\x08\x0a-\x1F]*?)\]',
        re.S | re.U)
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/__init__.py", line 289, in compile
    return _compile(pattern, flags)
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/__init__.py", line 350, in _compile
    p = _compiler.compile(pattern, flags)
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_compiler.py", line 762, in compile
    p = _parser.parse(p, flags)
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_parser.py", line 973, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_parser.py", line 460, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       not nested and not items))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_parser.py", line 856, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_parser.py", line 460, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       not nested and not items))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_parser.py", line 856, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_parser.py", line 460, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       not nested and not items))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/love-bird/anaconda3/envs/penguin-feather/lib/python3.14/re/_parser.py", line 834, in _parse
    raise source.error('global flags not at the start '
                       'of the expression',
                       source.tell() - start)
re.PatternError: global flags not at the start of the expression at position 4

The patch was for the regex, considering the recent recommendation on regex, regarding (?i) patterns.

I tested the patch locally for python 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, and 3.14.

I hope that it would be merged but even in such a case that it would not, I hope that one or two persons would find it helpful, if they encounter the same issue.

I tag seemingly related ones: #340 #342 #343 #348

@AngledLuffa

Copy link
Copy Markdown
Collaborator

I believe this was fixed with this PR:

#313

@emmanuel-stone

emmanuel-stone commented Jul 17, 2026

Copy link
Copy Markdown
Author

thank you for the merging of the #313 ! thank you very much for your efforts of the maintaining - again, I am such a big fan of your repo and use it all the time when I preprocess wiki ! with warm regards, emmanuel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants