在字符串c#中搜索特定的模式。

时间:2022-09-13 07:44:11

I got a string which can has a part which looks like this: "1.0.0" but it can also look like this "1.0.0.0" and these are the parts I need out of the string. I tried using this regular expression:

我有一个字符串,它可以有一个像这样的部分"1。0"但是它也可以像这个"1。0。0"这些是我需要的字符串的部分。我试着用这个正则表达式:

var dotPart = Regex.Match(infoString, @"(\d+)\.(\d+)\.(\d+)(\.d+)*");

This works fine on "1.0.0" but not on "1.0.0.0" which then returns only the first 3 digits instead of the last. How do I fix my pattern? And for general knowledge, what is wrong with the pattern I wrote?

这在“1.0.0”上很有效,但在“1.0.0.0”上就不行,因为“1.0.0”只返回前3位,而不是前3位。如何修正我的模式?对于一般的知识,我写的模式有什么问题?

2 个解决方案

#1


3  

You missed the last backslash in \d:

你错过了最后一个斜杠在\d:

var dotPart = Regex.Match(infoString, @"(\d+)\.(\d+)\.(\d+)(\.\d+)*");

#2


0  

This tool always helped me. RegEx builder

这个工具总是帮助我。正则表达式构建器

#1


3  

You missed the last backslash in \d:

你错过了最后一个斜杠在\d:

var dotPart = Regex.Match(infoString, @"(\d+)\.(\d+)\.(\d+)(\.\d+)*");

#2


0  

This tool always helped me. RegEx builder

这个工具总是帮助我。正则表达式构建器