By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

re.sub and re.MULTILINE

P: n/a
I feel like a complete idiot but I can't figure out why re.sub won't
match multiline strings:

This works:
>>re.search("^foo", "\nfoo", re.MULTILINE)
<_sre.SRE_Match object at 0x6c448>

This doesn't. No replacement:
>>re.sub("^foo", "bar", "\nfoo", re.MULTILINE)
'\nfoo'

Why?

Thanks,
nyenyec

Jan 9 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

nyenyec wrote:
I feel like a complete idiot but I can't figure out why re.sub won't
match multiline strings:

This works:
>re.search("^foo", "\nfoo", re.MULTILINE)
<_sre.SRE_Match object at 0x6c448>

This doesn't. No replacement:
>re.sub("^foo", "bar", "\nfoo", re.MULTILINE)
'\nfoo'

Why?

Thanks,
nyenyec
Check the arguments to re.sub.
>>re.sub('(?m)^foo', 'bar', '\nfoo', count=0)
'\nbar'
- Paddy.

Jan 9 '07 #2

P: n/a

Paddy wrote:
Check the arguments to re.sub.
>re.sub('(?m)^foo', 'bar', '\nfoo', count=0)
'\nbar'

- Paddy.
Duh! :) I appreciate it, thanks.

-- nyenyec

Jan 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.