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
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
>>> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With