【问题标题】:How do you check if a string contains an int at a specific location?如何检查字符串是否在特定位置包含 int?
【发布时间】:2019-06-25 10:31:30
【问题描述】:

我想在继续之前确保文件夹的名称格式正确。下面的代码演示了我正在尝试做的事情,尽管 {char.IsDigit} 不起作用。我想用意味着“任何数字”的东西替换 char.IsDigit。

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

谢谢

【问题讨论】:

    标签: c# regex string isinteger


    【解决方案1】:

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

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

    注意\d只匹配一个数字,如果可以超过1个数字

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

    然后收紧:

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

    参见regex demoits graph

    【讨论】:

    • 对不起,当我写那条消息时我不能,因为它说我必须等待 5 分钟
    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 2011-06-26
    • 2015-10-25
    相关资源
    最近更新 更多