problem with re.MULTILINE

Discussion in 'Python' started by Necronymouse, Oct 18, 2009.

  1. Necronymouse

    Necronymouse Guest

    Hello i ´ve got a little problem: I ´ve this text:
    http://openpaste.org/en/secret/17343/pass-python and I need to parse
    it. So i wrote this:

    >>>

    patternNode = re.compile("""
    # Node (\w*).*
    (.*)""", re.MULTILINE)


    with open("test.msg", "r") as file:
    testData = file.read()

    for Node in re.findall(patternNode, testData):
    print "Node:", Node[0]
    print Node
    <<<

    but it prints only one line from text. If i am using re.DOTALL it
    wouldn´t print anything.. So don´t you know whre the problem is?

    Sorry for my English - it´s not my native language...
     
    Necronymouse, Oct 18, 2009
    #1
    1. Advertising

  2. Necronymouse

    MRAB Guest

    Necronymouse wrote:
    > Hello i ´ve got a little problem: I ´ve this text:
    > http://openpaste.org/en/secret/17343/pass-python and I need to parse
    > it. So i wrote this:
    >
    > patternNode = re.compile("""
    > # Node (\w*).*
    > (.*)""", re.MULTILINE)
    >
    >
    > with open("test.msg", "r") as file:
    > testData = file.read()
    >
    > for Node in re.findall(patternNode, testData):
    > print "Node:", Node[0]
    > print Node
    > <<<
    >
    > but it prints only one line from text. If i am using re.DOTALL it
    > wouldn´t print anything.. So don´t you know whre the problem is?
    >

    I assume you mean that it's giving you only the first line of text of
    each node.

    "(.*)" will capture a single (and possibly empty) line of text.

    "(.+\n)" will capture a single non-empty line of text ending with a
    newline.

    I think you want to capture multiple non-empty lines, each line ending
    with a newline:

    patternNode = re.compile("""
    # Node (\w*).*
    ((?:.+\n)*)""", re.MULTILINE)

    > Sorry for my English - it´s not my native language...


    It's better than my Czech/Slovak (depending on what Google says)! :)
     
    MRAB, Oct 18, 2009
    #2
    1. Advertising

  3. Necronymouse

    Necronymouse Guest

    On 18 říj, 21:20, MRAB <> wrote:
    > Necronymouse wrote:
    > > Hello i ´ve got a little problem: I ´ve this text:
    > >http://openpaste.org/en/secret/17343/pass-pythonand I need to parse
    > > it. So i wrote this:

    >
    > > patternNode = re.compile("""
    > > # Node (\w*).*
    > > (.*)""", re.MULTILINE)

    >
    > > with open("test.msg", "r") as file:
    > >     testData = file.read()

    >
    > > for Node in re.findall(patternNode, testData):
    > >     print "Node:", Node[0]
    > >     print Node
    > > <<<

    >
    > > but it prints only one line from text. If i am using re.DOTALL it
    > > wouldn´t print anything.. So don´t you know whre the problem is?

    >
    > I assume you mean that it's giving you only the first line of text of
    > each node.
    >
    > "(.*)" will capture a single (and possibly empty) line of text.
    >
    > "(.+\n)" will capture a single non-empty line of text ending with a
    > newline.
    >
    > I think you want to capture multiple non-empty lines, each line ending
    > with a newline:
    >
    > patternNode = re.compile("""
    > # Node (\w*).*
    > ((?:.+\n)*)""", re.MULTILINE)
    >
    > > Sorry for my English - it´s not my native language...

    >
    > It's better than my Czech/Slovak (depending on what Google says)! :)


    Yeah this works ( ((?:.+\r\n)*) ), thanks.. It´s czech..
     
    Necronymouse, Oct 18, 2009
    #3
    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. Joel Finkel

    Problem with MultiLine TextBox

    Joel Finkel, Nov 8, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    756
    Joel Finkel
    Nov 11, 2003
  2. Jack Wright

    Problem in CRLF in multiline fields

    Jack Wright, Apr 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    410
    Jack Wright
    Apr 21, 2004
  3. angus
    Replies:
    0
    Views:
    1,156
    angus
    May 20, 2004
  4. Antonio Lopez Arredondo

    simple problem with multiline textboxes in ASP.NET.....

    Antonio Lopez Arredondo, Nov 26, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,108
    Jason MacKenzie
    Nov 26, 2004
  5. dale zhang
    Replies:
    8
    Views:
    451
    Tintin
    Nov 30, 2004
Loading...

Share This Page