Skip to content

re.sub is only replacing max. of 2 string found by regexp. #79724

Closed

Description

@saga
mannequin
BPO 35543
Nosy @serhiy-storchaka

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 2018-12-20.08:32:12.140>
created_at = <Date 2018-12-20.08:26:30.724>
labels = ['invalid', 'type-bug', 'library']
title = 're.sub is only replacing max. of 2 string found by regexp.'
updated_at = <Date 2018-12-20.08:32:12.138>
user = 'https://bugs.python.org/saga'

bugs.python.org fields:

activity = <Date 2018-12-20.08:32:12.138>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2018-12-20.08:32:12.140>
closer = 'serhiy.storchaka'
components = ['Library (Lib)']
creation = <Date 2018-12-20.08:26:30.724>
creator = 'saga'
dependencies = []
files = []
hgrepos = []
issue_num = 35543
keywords = []
message_count = 2.0
messages = ['332198', '332200']
nosy_count = 2.0
nosy_names = ['serhiy.storchaka', 'saga']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue35543'
versions = ['Python 3.5']

Activity

  1. saga commented on Dec 20, 2018

    sagamannequin
    MannequinAuthor

    Below are the logs:

    >>> dat = '"10GE" "4x" "AMPC" "B3" "BUILTIN" "DOWN" "LU" "SFP+" "ether" "xe" "DOWN" "MPC" "BUILTIN"'
    >>> type = re.subn(r'\"BUILTIN\"|\"B\d\"|\"I\d\"|\"LU\"|\"Trinity\"|\"Trio\"|\"DOWN\"|\"UNKNOWN\"|'
    ...     r'^AND$|\"Q\"|\"MPC\"|\"EA\d\"|\"3D\"', '', dat, re.I)
    >>> type
    ('"10GE" "4x" "AMPC"   "DOWN" "LU" "SFP+" "ether" "xe" "DOWN" "MPC" "BUILTIN"', 2)
    >>> dat = '"10GE" "4x" "AMPC"   "DOWN" "LU" "SFP+" "ether" "xe" "DOWN" "MPC" "BUILTIN"'
    >>> type = re.subn(r'\"BUILTIN\"|\"B\d\"|\"I\d\"|\"LU\"|\"Trinity\"|\"Trio\"|\"DOWN\"|\"UNKNOWN\"|'
    ...     r'^AND$|\"Q\"|\"MPC\"|\"EA\d\"|\"3D\"', '', dat, re.I)
    >>> type
    ('"10GE" "4x" "AMPC"     "SFP+" "ether" "xe" "DOWN" "MPC" "BUILTIN"', 2)
    >>> dat = '"10GE" "4x" "AMPC"     "SFP+" "ether" "xe" "DOWN" "MPC" "BUILTIN"'
    >>> type = re.subn(r'\"BUILTIN\"|\"B\d\"|\"I\d\"|\"LU\"|\"Trinity\"|\"Trio\"|\"DOWN\"|\"UNKNOWN\"|'
    ...     r'^AND$|\"Q\"|\"MPC\"|\"EA\d\"|\"3D\"', '', dat, re.I)
    >>> type
    ('"10GE" "4x" "AMPC"     "SFP+" "ether" "xe"   "BUILTIN"', 2)
    >>> dat = '"10GE" "4x" "AMPC"     "SFP+" "ether" "xe"   "BUILTIN"'
    >>> type = re.subn(r'\"BUILTIN\"|\"B\d\"|\"I\d\"|\"LU\"|\"Trinity\"|\"Trio\"|\"DOWN\"|\"UNKNOWN\"|'
    ...     r'^AND$|\"Q\"|\"MPC\"|\"EA\d\"|\"3D\"', '', dat, re.I)
    >>> type
    ('"10GE" "4x" "AMPC"     "SFP+" "ether" "xe"   ', 1)
    >>>
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Dec 20, 2018
  3. serhiy-storchaka commented on Dec 20, 2018

    @serhiy-storchaka
    Member

    The third argument of re.sub() is the maximal number of replacements. re.I == 2.

    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.
  4. 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

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions