【问题标题】:Check entire string matches the RegEx or not [duplicate]检查整个字符串是否与正则表达式匹配 [重复]
【发布时间】:2015-01-08 19:25:50
【问题描述】:

我确实有以下代码

Regex rx = new Regex(@"(\w{2}(\,\w{2})*)+");

我不知道如何检查整个输入字符串是否属于该格式 例如

INPUT: W2 -> true
INPUT x3,4e -> true
INPUT x3,4e,33 -> true

INPUT: x -> false
INPUT x3,e -> false

我不需要找到任何匹配项!我只需要知道输入的格式是否正确。

谢谢

【问题讨论】:

标签: c# regex


【解决方案1】:

@史蒂夫霍华德和@Juharr

谢谢你们,辛苦了!

Regex rx = new Regex(@"^(\w{2}(\,\w{2})*)+$");
            string input = txtTermCodes.Text.Trim();
            if(rx.IsMatch(input))
                return true;
            else 
                return false;

【讨论】:

    【解决方案2】:

    您必须提取匹配的结果,这是一个字符串,然后将其长度与输入的长度进行比较,如下所示:

    Regex rx = new Regex(@"(\w{2}(\,\w{2})*)+");
    String input = "Your input";
    Match m = rx.Match(input);
    
    if (!m.Success) return false;
    
    return m.Length == input.Length;
    

    【讨论】:

    • 不行,试试dd,dd
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多