Regex pattern to extract version number from string

I want to extract the version number from a string.

a string = "Tale: The Secrets 1.6" b string=" The 34. Mask 1.6.98"; 

So for version number 1.6 and for b it's 1.6.98

+7
source share
4 answers
 \d+(\.\d+)+ 

\d+ : one or more digits
\. : one point
(\.\d+)+ : one or more occurrences of dot digits

I will find

 2.5 3.4.567 3.4.567.001 

But you will not find

 12 3. .23 

If you want to exclude decimal numbers such as 2.5 and expect the version number to be at least 3 parts, you can use a quantifier like this

 \d+(\.\d+){2,} 
+14
source

Try:

 Regex pattern = new Regex("\d+(\.\d+)+"); Match m = pattern.Match(a); string version = m.Value; 
+8
source

You can write

 [0-9]+(\.[0-9]+)+$ 

This should match the format. $ for matching at the end, can be discarded if not needed.

+4
source

By version number, do you mean any sequence of numbers alternating with dots?

 \d+(\.\d+)+ 
+3
source

Source: https://habr.com/ru/post/1392242/


All Articles