How do I cut out a piece of text?

Opinderjit bhellao at my-deja.com
Thu Sep 14 13:05:20 EDT 2000


Assuming that the entire VRML text below is contained in a variable, say
"VRML". The following regular expression should work. The re.S flag
makes the "." special character match any character including newlines.

The desired text should be contained in the group "faceB" or whatever
name you want to give it. If this is used often, you could compile this
pattern before hand and turn it into a generic function.

Hope this helps.

import re
re.search(r"(?P<faceB>\s+DEF\s+faceB\s+.*(?=DEF\s+faceC)", VRML, re.S)
print b.group("faceB")

The output:

   DEF faceB Transform {
   children Shape {
     appearance Appearance { material USE Testobjekt_0 }
     geometry USE faceB_0Geo
   }
   }


In article <8pq0kg$d20$1 at nyheter.chalmers.se>,
  "Fredrik Lundin" <m94lufr at mtek.chalmers.se> wrote:
> Hey!
>
> I want to cut out a piece of text and put it in a new file.
> Lets say that the text looks like this (VRML transform nodes):
>
>   DEF faceA Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceA_0Geo
>    }
>   }
>   DEF faceB Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceB_0Geo
>    }
>   }
>   DEF faceC Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceC_0Geo
>    }
>   }
>
> I would like to put this plart into a new file:
>
>   DEF faceB Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceB_0Geo
>    }
>   }
>
> Could someone help me with this?
> I have tried quite alot to accomplish it, but I can't seem to
eliminate the
> stuff ahead of the wanted text portion.
>
> /Fred
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list