Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with re.MULTILINE

Reply
Thread Tools

problem with re.MULTILINE

 
 
Necronymouse
Guest
Posts: n/a
 
      10-18-2009
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...
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      10-18-2009
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)!
 
Reply With Quote
 
 
 
 
Necronymouse
Guest
Posts: n/a
 
      10-18-2009
On 18 ┼Ö├*j, 21:20, MRAB <(E-Mail Removed)> 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..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments