如何检查字符串是否在特定位置包含int?

时间:2019-06-25 10:31:30

标签: c# regex string isinteger

在继续操作之前,我想确保文件夹的名称格式正确。尽管{char.IsDigit}不起作用,但是下面的代码演示了我正在尝试执行的操作。我想用“任何数字”代替char.IsDigit。

if(versionName == $"Release {char.IsDigit}.{char.IsDigit}.{char.IsDigit}.{char.IsDigit}")
{
    //Do something
}

谢谢

1 个答案:

答案 0 :(得分:5)

您想将Regex.IsMatch与正则表达式一起使用:

if(Regex.IsMatch(versionName, @"^Release \d\.\d\.\d\.\d$"))
{
    //Do something
}

注意\d仅匹配一位数字,如果可以超过一位数字

@"^Release \d+\.\d+\.\d+\.\d+$"

并将其全部收紧:

@"^Release \d+(?:\.\d+){3}$"

请参见regex demoits graph

enter image description here