简体   繁体   English

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

[英]How do you check if a string contains an int at a specific location?

I want to make sure that a folder has the correct name format before proceeding. 在继续之前,我想确保文件夹具有正确的名称格式。 The code below demonstrates what I am trying to do, although {char.IsDigit} doesn't work. 尽管{char.IsDigit}不起作用,但是下面的代码演示了我正在尝试执行的操作。 I would like to replace char.IsDigit with something that means "any digit". 我想用“任何数字”代替char.IsDigit。

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

Thanks 谢谢

You want to use Regex.IsMatch with a regex like: 您想将Regex.IsMatch与以下正则表达式一起使用:

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

Note \\d just matches a single digit, if there can be more than 1 digits 注意\\d仅匹配一个数字,如果可以超过1个数字

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

And tightening it all up: 并全部收紧:

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

See the regex demo and its graph : 参见regex演示 及其图形

在此处输入图片说明

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

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