Regular Expressions Problem

Andrew Dalke adalke at mindspring.com
Thu Sep 9 23:34:44 CEST 2004


Oriana wrote:
> 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

 >>> import re
 >>> pat = re.compile(r"^\*\s*\n(^\*\s*\n)+", re.MULTILINE)
 >>> text = """/**
... *
... * Project:      MyProject
... *
... *
... *
... *
... *
... *
... *
... * Description:
... *
... *    This file contains the some code.
... *
... * Public Functions:
... *
... *     function_1
... *     function_2
... *
... * Private Functions:
... *
... *    None.
... *
... *
... * Notes:
... """
 >>> print pat.sub("*\n", text)
/**
*
* Project:      MyProject
*
* Description:
*
*    This file contains the some code.
*
* Public Functions:
*
*     function_1
*     function_2
*
* Private Functions:
*
*    None.
*
* Notes:

 >>>

				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list