Need some assistance on Regular expression in python

MRAB python at mrabarnett.plus.com
Wed Apr 18 14:51:24 EDT 2012


On 18/04/2012 19:36, vijay swaminathan wrote:
> Hi Experts,
>
>
> I'm a new bie to python and need some assistance in the usage of regular
> expression.
>
> I have a string something like this:
>
> New builds available Version: 20120418-abcdef-1 (based on SDK 0.0.0.1)
>
> from the above string I want to extract the following text using regular
> expression
>
> 20120418-abcdef-1
> 0.0.0.1
>
> I can do this by split but I feel it is an ineffective way of doing
> this. I tried using regular expression but could not narrow down.
>
> for example, I used
>
>      sdk_version = re.search(r"SDK(.*)", lines,)
>                          print sdk_version.group(1)
>
> but this gave the version 0.0.0.1) along with the paranthesis I did not
> know how to elimate ')' .. Need some help here..
>
How about this:

>>> s = "New builds available Version: 20120418-abcdef-1 (based on SDK 0.0.0.1)"
>>> import re
>>> m = re.search(r"Version: (\w+(?:-\w+)*).*?\bSDK\s+(\d+(?:\.\d+)*)", s)
>>> m.group(1)
'20120418-abcdef-1'
>>> m.group(2)
'0.0.0.1'



More information about the Python-list mailing list