I want to extract version number from string.
a string = "Tale: The Secrets 1.6"
b string=" The 34. Mask 1.6.98";
So for a version number is 1.6 and for b is 1.6.98
I want to extract version number from string.
a string = "Tale: The Secrets 1.6"
b string=" The 34. Mask 1.6.98";
So for a version number is 1.6 and for b is 1.6.98
\d+(\.\d+)+
\d+ : one or more digits
\. : one point
(\.\d+)+ : one or more occurences of point-digits
Will find
2.5
3.4.567
3.4.567.001
But will not find
12
3.
.23
If you want to exclude decimal numbers like 2.5 and expect a version number to have at least 3 parts, you can use a quantifier like this
\d+(\.\d+){2,}
After the comma, you can specify a maximum number of ocurrences.
Try:
Regex pattern = new Regex("\d+(\.\d+)+");
Match m = pattern.Match(a);
string version = m.Value;
34..You can write
[0-9]+(\.[0-9]+)+$
This should match the format. The $ is for matching at the end, can be dropped if not needed.
_, \. should be replaced with _: [0-9]+(_[0-9]+)+$ (referring to this duplicate).