ImageImage

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: "\W" pattern with re.ASCII flag is not equivalent to "[^a-zA-Z0-9_]"
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, mrabarnett, owentrigueros, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-10-13 09:10 by owentrigueros, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg403810 - (view) Author: Owen (owentrigueros) Date: 2021-10-13 09:10
"\W" regex pattern, when used with `re.ASCII`, is expected to have the same behavior as "[^a-zA-Z0-9_]" (see [1]).

For example, the following `sub()` call

```
>>> re.sub('\W', '', '½ a', re.ASCII)
'½a'
```

should return the same as this one:

```
>>> re.sub('[^a-zA-Z0-9_]', '', '½ a', re.ASCII)
'a'
```

But it does not.

[1] https://docs.python.org/3/library/re.html#regular-expression-syntax
msg403811 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-13 09:28
It works as expected:

>>> re.sub(r'\W', '', '½ a', 0, re.ASCII)
'a'

You just passed re.ASCII as the count argument, not as the flags argument.

>>> help(re.sub)
Help on function sub in module re:

sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the Match object and must return
    a replacement string to be used.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89621
2021-10-13 09:28:14serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg403811

resolution: not a bug
stage: resolved
2021-10-13 09:10:37owentrigueroscreate