Skip to content

re.sub() does not work correctly on '.' pattern and \n #75110

New issue

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

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

Already on GitHub? # to your account

Closed
mrh1997 mannequin opened this issue Jul 13, 2017 · 2 comments
Closed

re.sub() does not work correctly on '.' pattern and \n #75110

mrh1997 mannequin opened this issue Jul 13, 2017 · 2 comments
Labels
topic-regex type-bug An unexpected behavior, bug, or error

Comments

@mrh1997
Copy link
Mannequin

mrh1997 mannequin commented Jul 13, 2017

BPO 30927
Nosy @ezio-melotti, @mrh1997

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 2017-07-13.23:58:25.716>
created_at = <Date 2017-07-13.21:27:56.387>
labels = ['expert-regex', 'type-bug', 'invalid']
title = "re.sub() does not work correctly on '.' pattern and \\n"
updated_at = <Date 2017-07-13.23:58:25.715>
user = 'https://github.com/mrh1997'

bugs.python.org fields:

activity = <Date 2017-07-13.23:58:25.715>
actor = 'mrabarnett'
assignee = 'none'
closed = True
closed_date = <Date 2017-07-13.23:58:25.716>
closer = 'mrabarnett'
components = ['Regular Expressions']
creation = <Date 2017-07-13.21:27:56.387>
creator = 'mrh1997'
dependencies = []
files = []
hgrepos = []
issue_num = 30927
keywords = []
message_count = 2.0
messages = ['298316', '298323']
nosy_count = 3.0
nosy_names = ['ezio.melotti', 'mrabarnett', 'mrh1997']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue30927'
versions = ['Python 3.6']

@mrh1997
Copy link
Mannequin Author

mrh1997 mannequin commented Jul 13, 2017

When running the command

    re.sub(r'X.', '+', '-X\n-', re.DOTALL)

you get '-X\n-' instead of '-+-'.

Curiously findall works correctly:

re.findall(r'X.', '-X\n-', re.DOTALL) => ['X\n']

@mrh1997 mrh1997 mannequin added topic-regex type-bug An unexpected behavior, bug, or error labels Jul 13, 2017
@mrabarnett
Copy link
Mannequin

mrabarnett mannequin commented Jul 13, 2017

The 4th parameter is the count, not the flags:

sub(pattern, repl, string, count=0, flags=0)
>>> re.sub(r'X.', '+', '-X\n-', flags=re.DOTALL)
'-+-'

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
topic-regex type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants