To answer all your questions:
So this doesn't because here the ".*" does not match the single space (why?).
it does match the space and anything, it is the greediest regexp
Again not what i need, because here, while "[ .]*" matches the space " ", it does not match " something " (again, why?)
.
inside [ ]
of a regexp matches the character dot only. It wouldn't neither make sense to match all characters when inside a characters selections set because other characters in the the specified set wouldn't mean anything if such interpreted dot was present in the set
How would the pattern need to look so i can match it to strings like "something_optional a:43 something_optional b:345.7" and "something_optional a:43 something_optional"?
if something optional between a: and b: is not totally empty, such as is not a:6b:9.3 but at least a space a tab a coma or anything consistent (which I think is the case), the following regexp would do it
r".*?a:(\d*).+?(?:b:(\d*\.\d*))?"
or (same also in python but without python r
prefix) ".*?a:(\\d*).+?(?:b:(\\d*\\.\\d*))?"
I would also recommend to rethink the floating point regular expression as you might prefer to match integer too as b:(\d*(?:\.\d*)?)
.
All that above works and efficiently.
import re
re.compile(r".*a:(\d*).*?(?:b:(\d*.\d*))?").match("prefix a:77 b:3.25").groups()
# ^^ ^ ^
# ('77', None)
#same regexp different data when the .*? doesn't kick in its matching
re.compile(r".*a:(\d*).*?(?:b:(\d*.\d*))?").match("prefix a:77b:3.25").groups()
# ('77', 3.25)
apparently to oblige to write a less efficient regexp as
r".*(?:a:(\d*).*?(?:b:(\d*(?:\.\d*)?))?|a:(\d+))" # a is in first xor third match
This last regular expression does what you literally asked with something_optional(including empty) between a: and b: but I'm unsatisfied by its sub optimal efficiency, for a case that in your question is more unintended than needed, but it could be in other context, I'm thinking to make it as a question and will update cross referencing