简体   繁体   中英

Regex pattern to extract version number from string

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;

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.

版本号是指任何散布点的数字序列吗?

\d+(\.\d+)+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM