473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

re.sub and re.MULTILINE

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
2 2943

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rasmus Fogh | last post by:
Dear All, I need a way of writing strings or arbitrary Python code that will a) allow the strings to be read again unchanged (like repr) b) write multiline strings as multiline strings instead...
1
by: David Elliott | last post by:
I have an expression that works for single line but not multiline. What am I missing? expression = "<div(?<data1>.*?)>(?<data2>.*?)</div>"; MatchCollection mc = Regex.Matches(data, expression,...
4
by: Michael C | last post by:
Hi all, I'm trying to add lines to a multiline textbox. Here's what I'd like to end up with in the textbox (as an example): Line1 Line2 Line3 I've tried a couple of methods but nothing...
2
by: C.McCullough | last post by:
It appears that the multiline and border properties of a tab control in the IDE on Visual Studio .Net 2003 are broken. I wanted to set the multiline property of my tab control to true to enable...
7
by: Joel Finkel | last post by:
Folks, I have a form that has several TextBoxes, some of which have the TextMode set to MultiLine. Each is pre-loaded with data from a database. The user is allowed to modify each entry. The...
2
by: Enzo Marinelli | last post by:
Hello everyone I have a MultiLine TextBox, as follows <asp:TextBo Id="StreetAddress Rows="4 Runat="Server TextMode="MultiLine Width="100%"/
40
by: Edward Elliott | last post by:
At the risk of flogging a dead horse, I'm wondering why Python doesn't have any multiline comments. One can abuse triple-quotes for that purpose, but that's obviously not what it's for and doesn't...
9
by: John Salerno | last post by:
How do you make a single string span multiple lines, but also allow yourself to indent the second (third, etc.) lines so that it lines up where you want it, without causing the newlines and tabs or...
6
by: Zdenek Maxa | last post by:
Hi all, I would like to perform regular expression replace (e.g. removing everything from within tags in a XML file) with multiple-line pattern. How can I do this? where =...
2
by: King | last post by:
Is there any other way to define multiline text in a XML file: <Help><!]> </Help>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.