nr: #3 dodano: 2017-01-06 08:01
@maxple's answer only works when the substring of interest is at the beginning of the string. As that was not part of the specification (only in the example), I don't think that's a reasonable assumption. (@Eric did not make that assumption.)
There is also ambiguity about your statement, "discard everything after the second decimal". @maxple interpreted that as after the second decimal point (but also discarded the second decimal point), whereas @Eric assumed it meant after the second decimal digit. This is what happens when questions are imprecise.
If the substring is at the beginning of the string, and you mean to discard the second decimal point and everything after, here are two ways to do that.
str = "1.14.2.ab3-4.dl0.rhel"
1. Modify @Eric's regex:
2. Convert the string to a float and then back to a string:
nil if the desired substring does not exist, whereas #2 returns
"0.0". As long as
"0.0" is not a valid substring, either can be used to determine if the substring exists, and if it does, return the substring.