[Tutor] Question about an re

Ed Singleton singletoned at gmail.com
Thu Oct 27 10:29:02 CEST 2005


OR tests for the truth of the first thing then the truth of the second.

The first part of your OR is always true, so it doesn't test the second.

What you really want to do is look for a digit and then look for more
digits or a slash and some more digits.

So:

: \d(\d+|/\d+)

but this is easier by just allowing an optional slash in the digits:

: \d+/?\d*

ie 1 or more digits followed by 0 or 1 slashes followed by 0 or more digits.

http://www.regular-expressions.info/ is a brilliant tutorial on regexs
and http://www.weitz.de/regex-coach/ is a great program for testing
them out before you put them in your code.

Hope this helps

Ed

On 27/10/05, ->Terry<- <tvbare at socket.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> os - Slackware
> py - 2.4.1
>
> I'm trying to grab the value 10 in
>
> Visibility: 10 mile(s):0
>
> but sometimes the value looks like
>
> Visibility: 1/2 mile(s):0
>
> My not working regex looks like
>
> re.compile(': \d+|: \d/\d')
>
> If I'm understanding right, this should match either or,
> but it doesn't work for the fractional reading.
>
> Can someone steer me in the right direction and explain
> what I'm doing wrong?
>
> Thanks much,
> - --
>      Terry
>
>       ,-~~-.___.     Terry Randall <tvbareATsocketDOTnet>
>      / |  '     \
>     <   )        0    Linux Counter Project User# 98233
>      \_/, ,-----'
>         ====          //
>        /  \-'~;    /~~~(0)
>       /  __/~|   /      |   If only Snoopy had Slackware...
>     =( ______| (________|
>
> "He is your friend, your partner, your defender, your dog.
> You are his life, his love, his leader. He will be yours,
> faithful and true, to the last beat of his heart. You owe
> it to him to be worthy of such devotion."    -- Unknown
>
>           (Best viewed with a mono-spaced font.)
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.7 (GNU/Linux)
>
> iD8DBQFDYFhdQvSnsfFzkV0RAoPyAJ4jfwjWJ1eoqDatB6Hmg07XbUMGLQCeP/eQ
> ognaRmmfxNlDW249jRqYE2g=
> =Txx/
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list