简体   繁体   English

在字符串C#中搜索特定模式

[英]Search for a specific pattern in a string C#

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. 我得到了一个字符串,它的一部分可能看起来像这样:“ 1.0.0”,但它也可能看起来像这样的“ 1.0.0.0”,这些是我需要从字符串中取出的部分。 I tried using this regular expression: 我尝试使用此正则表达式:

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. 这在“ 1.0.0”上工作正常,但在“ 1.0.0.0”上无效,然后仅返回前3位而不是最后3位。 How do I fix my pattern? 如何修复我的图案? And for general knowledge, what is wrong with the pattern I wrote? 对于一般知识,我编写的模式有什么问题?

您错过了\\d的最后一个反斜杠:

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

This tool always helped me. 这个工具总是对我有帮助。 RegEx builder 正则表达式生成器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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