Skip to content

re.sub() with re.MULTILINE not replacing all occurrences #59972

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

Closed
eacousineau mannequin opened this issue Aug 23, 2012 · 2 comments
Closed

re.sub() with re.MULTILINE not replacing all occurrences #59972

eacousineau mannequin opened this issue Aug 23, 2012 · 2 comments
Labels
topic-regex type-bug An unexpected behavior, bug, or error

Comments

@eacousineau
Copy link
Mannequin

eacousineau mannequin commented Aug 23, 2012

BPO 15768
Nosy @ezio-melotti

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 2012-08-23.00:05:02.788>
created_at = <Date 2012-08-23.00:03:04.857>
labels = ['expert-regex', 'type-bug', 'invalid']
title = 're.sub() with re.MULTILINE not replacing all occurrences'
updated_at = <Date 2012-08-23.00:05:02.786>
user = 'https://bugs.python.org/eacousineau'

bugs.python.org fields:

activity = <Date 2012-08-23.00:05:02.786>
actor = 'eacousineau'
assignee = 'none'
closed = True
closed_date = <Date 2012-08-23.00:05:02.788>
closer = 'eacousineau'
components = ['Regular Expressions']
creation = <Date 2012-08-23.00:03:04.857>
creator = 'eacousineau'
dependencies = []
files = []
hgrepos = []
issue_num = 15768
keywords = []
message_count = 2.0
messages = ['168909', '168910']
nosy_count = 3.0
nosy_names = ['ezio.melotti', 'mrabarnett', 'eacousineau']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue15768'
versions = ['Python 2.7']

@eacousineau
Copy link
Mannequin Author

eacousineau mannequin commented Aug 23, 2012

[Copying post I made here: http://stackoverflow.com/questions/12082886/bug-in-python-regex-re-sub-with-re-multiline]

I'm noticing some odd behavior in Python's Regex library, and I'm not sure if I'm doing something wrong.

If I run a regex on it using re.sub(), with re.MULTILINE. It seems to only replace the first few occurrences. It replaces all occurrences if I turn off re.MULTILINE, use re.subn(..., count = 0, flags = re.MULTILINE), or compile the regex using re.compile(..., re.MULTILINE).

I am running Python 2.7 on Ubuntu 12.04.

I've posted a random example on:

http://pastebin.com/49SU8Mm2 - Output from terminal
http://codepad.org/2RO2iS4O - Script, confirming behavior (except for re.subn(), which is different on 2.5)

@eacousineau eacousineau mannequin added topic-regex type-bug An unexpected behavior, bug, or error labels Aug 23, 2012
@eacousineau
Copy link
Mannequin Author

eacousineau mannequin commented Aug 23, 2012

Looks like I was just writing my code incorrectly - I should have been using re.sub(..., flags = re.MULTILINE).
That explains it.

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

No branches or pull requests

0 participants