matching patterns after regex?

Bernard bernard.chhun at gmail.com
Wed Aug 12 22:53:41 CEST 2009


On 12 août, 12:43, Martin <mdeka... at gmail.com> wrote:
> On Aug 12, 1:42 pm, Martin <mdeka... at gmail.com> wrote:
>
>
>
>
>
> > On Aug 12, 1:23 pm, Steven D'Aprano <st... at REMOVE-THIS-
>
> > cybersource.com.au> wrote:
> > > On Wed, 12 Aug 2009 05:12:22 -0700, Martin wrote:
> > > > I tried
>
> > > > re.findall((\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+),s)
>
> > > You need to put quotes around strings.
>
> > > In this case, because you're using regular expressions, you should use a
> > > raw string:
>
> > > re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
>
> > > will probably work.
>
> > > --
> > > Steven
>
> > Thanks I see.
>
> > so I tried it and if I use it as it is, it matches the first instance:
> > I
> > n [594]: re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
> > Out[594]: [('NORTHBOUNDINGCOORDINATE', '1')]
>
> > So I adjusted the first part of the regex, on the basis I could sub
> > NORTH for SOUTH etc.
>
> > In [595]: re.findall(r"(NORTHBOUNDINGCOORDINATE).*\s+VALUE\s+=\s([\d\.
> > \w-]+)",s)
> > Out[595]: [('NORTHBOUNDINGCOORDINATE', '1')]
>
> > But in both cases it doesn't return the decimal value rather the value
> > that comes after NUM_VAL = , rather than VALUE = ?
>
> I think I kind of got that to work...but I am clearly not quite
> understanding how it works as I tried to use it again to match
> something else.
>
> In this case I want to print the values 0.000000 and 2223901.039333
> from a string like this...
>
> YDim=1200\n\t\tUpperLeftPointMtrs=(0.000000,2223901.039333)\n\t\t
>
> I tried which I though was matching the statement and printing the
> decimal number after the equals sign??
>
> re.findall(r"(\w+UpperLeftPointMtrs)*=\s([\d\.\w-]+)", s)
>
> where s is the string
>
> Many thanks for the help

You have to do it with 2 matches in the same regex:

regex = r"UpperLeftPointMtrs=\(([\d\.]+),([\d\.]+)"

The first match  is before the , and the second one is after the , :)

You should probably learn how to play with regexes.
I personnaly use a visual tool called RX Toolkit[1] that comes with
Komodo IDE.

[1] http://docs.activestate.com/komodo/4.4/regex.html



More information about the Python-list mailing list