Regular Expressions Problem

Discussion in 'Python' started by Oriana, Sep 9, 2004.

  1. Oriana

    Oriana Guest

    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
    Oriana, Sep 9, 2004
    #1
    1. Advertising

  2. On 2004-09-09, Oriana <> wrote:
    > .....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:
    >>> string = """*

    .... *
    .... *
    .... *
    .... *
    .... *
    .... *
    .... """
    >>> foo = re.compile(r'(^\*\s*\n){2,}',re.MULTILINE)
    >>> foo.sub("*\n",string)

    '*\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/


    --
    Kirk Job-Sluder
    "The square-jawed homunculi of Tommy Hilfinger ads make every day an
    existential holocaust." --Scary Go Round
    Kirk Job-Sluder, Sep 9, 2004
    #2
    1. Advertising

  3. Oriana

    Andrew Dalke Guest

    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
    Andrew Dalke, Sep 9, 2004
    #3
  4. Oriana

    Brian Szmyd Guest

    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....
    >
    > /**
    > *
    > * 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
    Brian Szmyd, Sep 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    592
    Jay Douglas
    Aug 15, 2003
  2. Jeff
    Replies:
    1
    Views:
    1,227
    Joris Gillis
    Feb 25, 2005
  3. Gabriel Genellina
    Replies:
    2
    Views:
    260
    Miles
    Sep 25, 2007
  4. mikko.n

    Regular expressions (multiple match problem)

    mikko.n, Apr 2, 2008, in forum: C Programming
    Replies:
    5
    Views:
    466
    Antoninus Twink
    Apr 2, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    219
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page