Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
saga mannequin opened this issue Dec 20, 2018 · 2 comments
Closed

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

saga mannequin opened this issue Dec 20, 2018 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@saga
Copy link
Mannequin

saga mannequin commented Dec 20, 2018

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']

@saga
Copy link
Mannequin Author

saga mannequin commented Dec 20, 2018

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)
>>>

@saga saga mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Dec 20, 2018
@serhiy-storchaka
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant