Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression usage in python

Tags:

python

regex

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..

Thanks -Vijay

like image 939
user596922 Avatar asked Jan 17 '23 21:01

user596922


2 Answers

Assuming version numbers can only contain letters, digits, dots and dashes, this is all you need:

version, sdk = re.findall(r'(?:Version: |SDK )([\w.-]+)', s)

Example:

s = "New builds available Version: 20120418-abcdef-1 (based on SDK 0.0.0.1)"

import re
version, sdk = re.findall(r'(?:Version: |SDK )([\w.-]+)', s)

print version
print sdk

## 20120418-abcdef-1
## 0.0.0.1
like image 95
georg Avatar answered Jan 19 '23 10:01

georg


>>> s = "New builds available Version: 20120418-abcdef-1 (based on SDK 0.0.0.1)"
>>> import re
>>> version = re.compile(r"(?<=Version: )\d*-[a-z]*-\d")
>>> version.search(s).group()
'20120418-abcdef-1'

This matches

(preceded by "Version: ")
a string of digits
hyphen
a string of lowercase letters
hyphen
a digit

Similarly,

>>> subversion = re.compile(r"(?<=SDK )\d*.\d*.\d*.\d*")
>>> subversion.search(s).group()
'0.0.0.1'

matches

(preceded by "SDK ")
a string of digits
a dot
a string of digits
a dot
a string of digits
a dot
a string of digits
like image 26
Katriel Avatar answered Jan 19 '23 11:01

Katriel