I have the following string s = “~ VERSION 11 11 11.1 222 22 22.222”
I Want to extract the following into the following variables:
string Variable1 = "11 11 11.1"
string Variable2 = "222 22 22.222"
How do I extract this with regular expression? Or is there a better alternative way? (note, There may be variable spacing in between the the tokens I want to extract and the leading character may be something other than a ~, but it will definitely be a symbol:
e.g. could be:
~ VERSION 11 11 11.1 222 22 22.222
$ VERSION 11 11 11.1 222 22 22.222
@ VERSION 11 11 11.1 222 22 22.222
If regular expression does not make sense for this or if there is a better way, please recommend.
How do I preform the extraction into those two variables in python?
Try this:
which gives output:
Note the use of verbose regular expressions with comments.
To convert the extracted version numbers to their numeric representation (i.e. int, float) use the regexp in @Preet Kukreti’s answer and convert using
int()orfloat()as suggested.