Skip to content

"\W" pattern with re.ASCII flag is not equivalent to "[^a-zA-Z0-9_]" #89621

Closed

Description

@owentrigueros
BPO 45458
Nosy @ezio-melotti, @serhiy-storchaka, @owentrigueros

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-10-13.09:28:14.946>
created_at = <Date 2021-10-13.09:10:37.218>
labels = ['expert-regex', 'type-bug', '3.8', '3.9', '3.10', '3.7', 'invalid']
title = '"\\W" pattern with re.ASCII flag is not equivalent to "[^a-zA-Z0-9_]"'
updated_at = <Date 2021-10-13.09:28:14.945>
user = 'https://github.com/owentrigueros'

bugs.python.org fields:

activity = <Date 2021-10-13.09:28:14.945>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2021-10-13.09:28:14.946>
closer = 'serhiy.storchaka'
components = ['Regular Expressions']
creation = <Date 2021-10-13.09:10:37.218>
creator = 'owentrigueros'
dependencies = []
files = []
hgrepos = []
issue_num = 45458
keywords = []
message_count = 2.0
messages = ['403810', '403811']
nosy_count = 4.0
nosy_names = ['ezio.melotti', 'mrabarnett', 'serhiy.storchaka', 'owentrigueros']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45458'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

Activity

  1. owentrigueros commented on Oct 13, 2021

    owentriguerosmannequin
    MannequinAuthor

    "\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

  2. serhiy-storchaka commented on Oct 13, 2021

    @serhiy-storchaka
    Member

    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.
  3. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions