469,951 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

Regular expressions and sub

Hey hey --

I've got a question about regular expression substitions. Basically,
I want to do something like this:

re.sub(r"(stuff)|(blah)", r"I was \00", "stuff blah")

Which would yield the string "I was stuff I was blah" -- but I get "I
was \x00 I was \x00" because apparently the replace string is simply
being interpreted as a normal string, and not a regular expression.

So, my question is, is there a simple way to do what I want?

Thanks much,
John
Jul 18 '05 #1
1 1290
On Thu, 2004-03-25 at 21:44, Emhoff wrote:
Hey hey --

I've got a question about regular expression substitions. Basically,
I want to do something like this:

re.sub(r"(stuff)|(blah)", r"I was \00", "stuff blah")

Which would yield the string "I was stuff I was blah" -- but I get "I
was \x00 I was \x00" because apparently the replace string is simply
being interpreted as a normal string, and not a regular expression.

So, my question is, is there a simple way to do what I want?

Thanks much,
John


Here's one way:
re.sub(r"(stuff|blah)", r"I was \1", "stuff blah")

'I was stuff I was blah'

Rich
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Michael McGarry | last post: by
2 posts views Thread by Sehboo | last post: by
4 posts views Thread by Egyd Csaba | last post: by
7 posts views Thread by Billa | last post: by
3 posts views Thread by a | last post: by
25 posts views Thread by Mike | last post: by
13 posts views Thread by Wiseman | last post: by
12 posts views Thread by FAQEditor | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.