[Tutor] Help With Regular expression? - Partial solution

Kent Johnson kent_johnson at skillsoft.com
Fri Sep 3 14:12:09 CEST 2004


If I understand you correctly, you want to match an open parenthesis 
followed by one or more digits, optionally followed by any number of (one 
or more whitespace followed by one or more digits) followed by a closing 
parenthesis

This regex will do just that:
\(\d+(\s+\d+)*\)

Taking a closer look:
\( - open paren
\d+ - one or more digits
(\s+\d+)* - one or more whitespace followed by one or more digits, repeated 
zero or more times
\) - closing parenthesis

This regex will recognize the line of interest, it won't help you extract 
the numbers. For that you could use re.findall, or maybe split() is enough.

BTW Python includes a handy regex tester, it is in 
Python/Tools/Scripts/redemo.py

Kent

At 10:42 AM 9/3/2004 +0300, Karthikesh Raju wrote:

>Hi All,
>
>i found a partial solution somthing like
>
>p = re.compile('\( [0-9]+  [0-9]+ \)')
>
>this will match (5 6), or any tuple with 2 indices, i want to put an
>optional part something like
>
>('\( [0-9]+ ([0-9]?+) \)')
>so that i can match any thing of the form
>( 5 ) ( 5 6 ) (5 6 7) and so on ..
>
>How should one craft it ..
>
>warm regards
>
>karthik
>-----------------------------------------------------------------------
>Karthikesh Raju,                    email: karthik at james.hut.fi
>                                            karthikesh.raju at gmail.com
>Researcher,                         http://www.cis.hut.fi/karthik
>Helsinki University of Technology,  Tel: +358-9-451 5389
>Laboratory of Comp. & Info. Sc.,    Fax: +358-9-451 3277
>Department of Computer Sc.,
>P.O Box 5400, FIN 02015 HUT,
>Espoo, FINLAND
>-----------------------------------------------------------------------
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list