Regular Expressions

O

Oriana

hi!

I've been working with Python for about two months now but I just
started learning about regular expressions.
My question is the following. I have a source file that contains
code and comments. A piece of the comments contains a revision history
that I would like to erase completely from the file since I never
update it and I don't use it anymore. The file looks something like

/****************************************
* Some info
*
* More info
*
*----------------------------------------
*
* Revision History:
*
* blah
* blah
* blah
*
*----------------------------------------


I would like to write some sort of regular expression to replace all
the text from the first dotted line up to the first asterisk that it's
followed either by another asterisk or by another *----
line.....please help, I don't know where to start!!!
 
J

Jeremy Jones

Oriana said:
hi!

I've been working with Python for about two months now but I just
started learning about regular expressions.
My question is the following. I have a source file that contains
code and comments. A piece of the comments contains a revision history
that I would like to erase completely from the file since I never
update it and I don't use it anymore. The file looks something like

/****************************************
* Some info
*
* More info
*
*----------------------------------------
*
* Revision History:
*
* blah
* blah
* blah
*
*----------------------------------------


I would like to write some sort of regular expression to replace all
the text from the first dotted line up to the first asterisk that it's
followed either by another asterisk or by another *----
line.....please help, I don't know where to start!!!
On this particular problem, you might be better served to not use
regexes to match the whole thing. The way that I would go about it
(this is obviously not the only answer, but it'll work) is to read in
the file line by line and rely on state variables to let you know when
to write out and when not to write out your current line. This is kinda
sketchy, but you get the idea. HTH


Jeremy
 
M

Max

On Wednesday 25 August 2004 18:02, Oriana wrote:

Hi,
I would like to write some sort of regular expression to replace all
the text from the first dotted line up to the first asterisk that it's
followed either by another asterisk or by another *----
line.....please help, I don't know where to start!!!


Below is some code that will search for the starting and ending *--- lines in
a specified file and print the whole file with the *--- part being removed.

Hope thats a start,
Max

#!/usr/bin/python

import re

f = file("somefile.c", "r")
text = f.read()
f.close()

m = re.search("\*-*.*\*-*\n", text, re.DOTALL)

if not m:
print "nothing found"
else:
print text[:m.start(0)] + text[m.end(0):]
 
J

Johannes Mockenhaupt

hi!

I've been working with Python for about two months now but I just
started learning about regular expressions.

I found this to be a good start:
http://www.amk.ca/python/howto/regex/
it's a "Regular Expressions With Python" Howto :)

[...]

Hope this helps,
Johannes
--
3. The most fun you can have with your clothes on.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBLQdE0q82ZtpaTRIRAhWHAKC6kZM/Dxg/6954t33DFZOw/7WvjQCfUbiw
Zrq3zLjhynnxIwzdJAOwe8M=
=/5vS
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top