Hi all,
I'm trying to do the following from within a code module:
import re
# text to match
text = "Good morning x something /x, how are you today x something
else /x"
# pattern to match
regex = re.compile("(x)(.*?)(/x)", re.DOTALL)
# find first match
m = re.search(regex, text)
# keep looking while there are matches
while m != None:
# substitute in some other text for the exact match
text = re.sub(m.group(), "Mr. Phelps", text)
# find the next match
m = re.search(regex, text)
print text
This works within the Python shell, but I can't seem to make it work
from within a program. It would seem like the re.sub(m.group()...)
would work fine as m.group() just returns the matched string
completely.
Any help would be greatly appreciated,
Doug Farrell 1 3132
Hi all,
I got a direct email from someone (who I can't remember right now) who
suggested that I wasn't very clear about what I was trying to do and
what was wrong. In an attempt to clarify I'm adding this post. :)
I'm trying to create a little Python CGI program that will read in a
HTML template file that has Python code embedded in it between
<python> ... </python> tags. So I'd like to use the re module to find
the following pattern:
pattern = re.compile("(<python>)(.*?)(</python>)", re.DOTALL)
Doing a search with this pattern like this:
match = re.search(pattern, text)
where text equals the HTML template file in string form, will find all
the code segments. I then pull out the code segment and pass it to
exec() to execute it. I can capture the output of the exec() call by
redirecting sys.stdout to a StringIO object. The problem is that this;
I can match the <python>...</python> segments no problem. But I only
want to replace each one in turn with the results of the exec() call.
If I do this:
text = re.sub(pattern, ".. exec() output..", text) it replaces all the
code segments, not just the one that was matched. I need to substitute
in the output of the exec() (which is a string when I'm done) into the
one place it came from.
Hope this clears up any confusion.
I didn't include the whole program in my first post as it's kind of
long. I thought the sections I included above were enough information
to follow what I'm trying to do, at least I hope so.
Thanks again in advance for your help,
Doug This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: James S |
last post by:
Hi,
Basically I've been fighting with this code for a few days now and
can't seem to work around this problem. Included is the output, the
program I use to get this error and the source code for...
|
by: Ashok |
last post by:
I am having some problem with the substitution operation. This is what
I am trying to do:
Get a directory path from the user and also the search string in the
path and remove the string from the...
|
by: Michael Hoffman |
last post by:
Many of you are familiar with Jason Orendorff's path module
<http://www.jorendorff.com/articles/python/path/>, which is frequently
recommended here on c.l.p. I submitted an RFE to add it to the...
|
by: Murali |
last post by:
In Python, dictionaries can have any hashable value as a string. In
particular I can say
d = {}
d = "Right"
d = "Wrong"
d = "test"
In order to print "test" using % substitution I can say
|
by: BartlebyScrivener |
last post by:
I know this must have been answered a hundred times, but I must be
searching on the wrong terminology.
Let's say I have a module foo.py that imports os.
I make another script called bar.py...
|
by: Steve |
last post by:
Hi All,
I'm having a tough time converting the following regex.compile patterns
into the new re.compile format. There is also a differences in the
regsub.sub() vs. re.sub()
Could anyone lend...
|
by: rjcarr |
last post by:
Sorry if this is a completely newbie question ...
I was trying to get information about the logging.handlers module, so
I imported logging, and tried dir(logging.handlers), but got:
...
|
by: Peter J. Bismuti |
last post by:
I'm having trouble understanding how namespaces work in modules. I want to
execute a module within the interpreter and then have values that are
calculated persist so that other modules that get...
|
by: Joe Strout |
last post by:
Some corrections, to highlight the depth of my confusion...
On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:
Actually, it does not.
And no, it isn't; it's the NAME of the module the...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |