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
Python Regex – Get List of all Numbers from String. To get the list of all numbers in a String, use the regular expression '[0-9]+' with re. findall() method. [0-9] represents a regular expression to match a single digit in the string.
RegEx uses metacharacters in conjunction with a search engine to retrieve specific patterns. Metacharacters are the building blocks of regular expressions. For example, “\d” in a regular expression is a metacharacter that represents a digit character.
Validating Numeric Ranges. If you're using the regular expression to validate input, you'll probably want to check that the entire input consists of a valid number. To do this, replace the word boundaries with anchors to match the start and end of the string: ^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$.
\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;
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.
By version number, do you mean any sequence of digits interspersed with dots?
\d+(\.\d+)+
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