Regular Expressions Problem

O

Oriana

Hi!

I'm trying to 'clean up' this source file using regular expressions
in Python. My problem is, that when I try to delete extra lines, my
code fails. Here's an example....

/**
*
* Project: MyProject
*
*
*
*
*
*
*
* Description:
*
* This file contains the some code.
*
* Public Functions:
*
* function_1
* function_2
*
* Private Functions:
*
* None.
*
*
* Notes:
*
* None.
*
*
*
*************************************************************************/


......I would like my code to only have one * space between lines, and
not all that white space that I see there. I tried to use the regular
expression: '^\*\n$^\*\n$' but that does not work. I've tried a bunch
of things and none of them seem to work....please help!!! Thanks in
advance, Oriana
 
K

Kirk Job-Sluder

.....I would like my code to only have one * space between lines, and
not all that white space that I see there. I tried to use the regular
expression: '^\*\n$^\*\n$' but that does not work. I've tried a bunch
of things and none of them seem to work....please help!!! Thanks in
advance, Oriana

Hrm, some suggestions.

First, you need to set the MULTILINE mode on the regular expression
object. You can do this with re.compile(pattern,re.MULTILINE).

Secondly, the "$" character matches just before the newline. So it
should be '^\*$\n'. In MULTILINE mode

Third, the regex you have here will reduce two blank comment lines to
one.

Try this:.... *
.... *
.... *
.... *
.... *
.... *
.... """'*\n'

The blank comment line is described by (^\*\s*\n) (asterisk at the start
of a line, followed by 0 or more space characters, then a newline).
The {2,} says "match two or more of this group."

Also, I can't really overrecommend "Mastering Regular Expressions" as a
good book for regular expression users:
http://www.oreilly.com/catalog/regex/

There is also a nice python-centric regex page at:
http://www.amk.ca/python/howto/regex/
 
A

Andrew Dalke

Oriana said:
Hi!

I'm trying to 'clean up' this source file using regular expressions
in Python. My problem is, that when I try to delete extra lines, my
code fails. Here's an example....

You probably need the re.MULTILINE flag. This worked for me
.... *
.... * Project: MyProject
.... *
.... *
.... *
.... *
.... *
.... *
.... *
.... * Description:
.... *
.... * This file contains the some code.
.... *
.... * Public Functions:
.... *
.... * function_1
.... * function_2
.... *
.... * Private Functions:
.... *
.... * None.
.... *
.... *
.... * Notes:
.... """/**
*
* Project: MyProject
*
* Description:
*
* This file contains the some code.
*
* Public Functions:
*
* function_1
* function_2
*
* Private Functions:
*
* None.
*
* Notes:

Andrew
(e-mail address removed)
 
B

Brian Szmyd

Oriana said:
Hi!

I'm trying to 'clean up' this source file using regular expressions
in Python. My problem is, that when I try to delete extra lines, my
code fails. Here's an example....

/**
*
* Project: MyProject
*
*
*
*
*
*
*
* Description:
*
* This file contains the some code.
*
* Public Functions:
*
* function_1
* function_2
*
* Private Functions:
*
* None.
*
*
* Notes:
*
* None.
*
*
*
*************************************************************************/


.....I would like my code to only have one * space between lines, and
not all that white space that I see there. I tried to use the regular
expression: '^\*\n$^\*\n$' but that does not work. I've tried a bunch
of things and none of them seem to work....please help!!! Thanks in
advance, Oriana

Are you reading in the file line by line? If so, why not just have a flag
that states you've seen a empty line, and then if the flag is true, do not
output any more empty lines till you see a non-emtpy line?

Pseudo-Code:

fp = openfile($filename)
op = openfine($newfile)

emptyline = 0
while(not fp.eof())
line = fp.readline()

if (isEmpty(line))
if(emptyline) continue
emptyline = 1
else
emptyline = 0

op.writeline(line)

Of course you'll have to decide how you want isEmpty() to decide if a string
is an empty line, but this should be pretty painless.

-regards
brian szmyd
 

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

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top