Domanda

input = """
endless gibberish
some more stuff

color texture mytexture
    [640 480 1]
    'BE4C16FFBD4B15FFBD4B15FFBD4B15FFBD4B15FFBE4C16FFBE4C16FFBD4B15FFBD4B15FF'
    'BE4C16FFBE4C16FFBD4B15FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBE4C16FF'
    'BF4C16FFBF4C16FFBE4B15FFBE4B15FFBC4913FFBC4913FFBC4913FFBB4812FFBC4913FF'
    'BC4A14FFBB4913FFBB4812FFBB4812FFBA4812FFBA4812FFBB4913FFBC4A16FFBB4915FF'
    'B84612FFB84612FFB94713FFB84612FFB64410FFB64410FFB64410FFB4420EFFB3410DFF'
    'FB03E0AFFB13F0BFB13F0BFFAE3C08FFAA3804FFAD3B07FFB03E0AFFB3410DFFB4420EFF'
    'B4400DFFB13D0AFFB23C0AFFB03C09FFB23E0BFFB5410EFFB74310FFB94512FFB84411FF'

color texture mytexture2
    [640 480 1]
    'BE4C16FFBD4B15FFBD4B15FFBD4B15FFBD4B15FFBE4C16FFBE4C16FFBD4B15FFBD4B15FF'
    'BE4C16FFBE4C16FFBD4B15FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBE4C16FF'
    (... etc...)

"""

Need to get the stuff between the brackets and the binary blob data after the line with "texture" until the empty line is reached. There's several "texture" paragraphs.

Here's what I got thus far:

p = re.compile(r'texture\s+(\S+)\s+\[(\d+\s+\d+\s+\d+)\]\s+(\'.+\')')
matches = p.findall(data)
    for match in matches:
         print match[0]
         print match[1]
         print match[2]
         print "---------------"

Gives the following output:

 mytexture
 640 480 1
 'BE4C16FFBD4B15FFBD4B15FFBD4B15FFBD4B15FFBE4C16FFBE4C16FFBD4B15FFBD4B15FF'
 ---------------
 mytexture2
 640 480 1
 'BE4C16FFBD4B15FFBD4B15FFBD4B15FFBD4B15FFBE4C16FFBE4C16FFBD4B15FFBD4B15FF'
 ---------------

I'm pretty sure re.MULTILINE should be used to get the whole blob, but its unclear to me how to grab all binary lines. My question basically is: how does one grab multiple lines and know when to "stop" (ie: the empty line is reached).

È stato utile?

Soluzione

re.MULTILINE affects the meaning of the ^ and $ anchors. What I think you want here is re.DOTALL, without which the . character will never match a newline.

To match all of the text up to the next blank line, you'd use something like (.*?)\n\s*\n. This seems to do what you're looking for?

p = re.compile(r'texture\s+(\S+)\s+\[(\d+\s+\d+\s+\d+)\]\s+(.*?)\n\s*\n', re.DOTALL)
matches = p.findall(input)
for match in matches:
    print match[0]
    print match[1]
    print match[2]
    print "---------------"

On your sample text, this produces:

mytexture
640 480 1
'BE4C16FFBD4B15FFBD4B15FFBD4B15FFBD4B15FFBE4C16FFBE4C16FFBD4B15FFBD4B15FF'
    'BE4C16FFBE4C16FFBD4B15FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBE4C16FF'
    'BF4C16FFBF4C16FFBE4B15FFBE4B15FFBC4913FFBC4913FFBC4913FFBB4812FFBC4913FF'
    'BC4A14FFBB4913FFBB4812FFBB4812FFBA4812FFBA4812FFBB4913FFBC4A16FFBB4915FF'
    'B84612FFB84612FFB94713FFB84612FFB64410FFB64410FFB64410FFB4420EFFB3410DFF'
    'FB03E0AFFB13F0BFB13F0BFFAE3C08FFAA3804FFAD3B07FFB03E0AFFB3410DFFB4420EFF'
    'B4400DFFB13D0AFFB23C0AFFB03C09FFB23E0BFFB5410EFFB74310FFB94512FFB84411FF'
---------------
mytexture2
640 480 1
'BE4C16FFBD4B15FFBD4B15FFBD4B15FFBD4B15FFBE4C16FFBE4C16FFBD4B15FFBD4B15FF'
    'BE4C16FFBE4C16FFBD4B15FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBC4A14FFBE4C16FF'
    (... etc...)
---------------

Altri suggerimenti

You were spot on, just didn't get so far as to actually implement it ;)

import re

input = """
endless gibberish
some more stuff

texture mytexture
    '01AB01AB01AB01BA'
    '01AB01AB01AB01BA'
    '01AB01AB01AB01BA'
    '01AB01AB01AB01BA'

"""

matches = re.findall(r'01AB01AB01AB01BA', input, re.M)
print matches
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top