Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不能以下划线开头的空格分隔字符串的正则表达式_C#_Regex - Fatal编程技术网

C# 不能以下划线开头的空格分隔字符串的正则表达式

C# 不能以下划线开头的空格分隔字符串的正则表达式,c#,regex,C#,Regex,我想验证标签列表——用空格分隔的字符串。示例: "blue white green123 #$#! ()!!!123 q_w_e_r_t_y" 唯一的要求是它们不能以下划线“\u1”开头。什么是匹配这些标记的适当正则表达式 我编写了一些测试用例来验证模式的正确性: public void RegexTest() { //arrange const string pattern = @"^PATTERN$"; var regex = new Regex(patte

我想验证标签列表——用空格分隔的字符串。示例:

"blue white   green123 #$#! ()!!!123  q_w_e_r_t_y"
唯一的要求是它们不能以下划线“\u1”开头。什么是匹配这些标记的适当正则表达式


我编写了一些测试用例来验证模式的正确性:

public void RegexTest()
{
    //arrange
    const string pattern = @"^PATTERN$";
    var regex = new Regex(pattern);
    var positive = new[] { "AAA", "A_", "AAA AAA", "AAA_AAAA", "AAA_AA AAA_aaa AA___   AAA", "A____", "333A%#$%#@%$__=-21-2-AA213", "+=-_0987654321`!@#$%^&*() qwertyu:/.," };
    var negative = new[] { "_AAAA", "A _AA ", "AA _AA", "A B _C", "_ " };

    //act
    var positiveMatches = positive.Select(x => regex.IsMatch(x)).ToArray();
    var negativeMatches = negative.Select(x => regex.IsMatch(x)).ToArray();

    //assert
    CollectionAssert.AreEqual(positiveMatches.Select(x => true).ToArray(), positiveMatches);
    CollectionAssert.AreEqual(new bool[negativeMatches.Length], negativeMatches);
}

只需使用一个消极的前瞻来检查没有空间或起始锚的未得分

^(?:(?!\s_|^_).)*$

只需使用一个负向前看来检查不在空间或起始锚之前的未得分

^(?:(?!\s_|^_).)*$

试试这个。看演示

试试这个。看演示


此正则表达式应在以下情况下工作:

^(?!(.*\s+)?_\w*\b).+$

这个正则表达式应该可以:

^(?!(.*\s+)?_\w*\b).+$

AA AA应拒绝此项或AA应返回编辑应拒绝,因为它包含以下划线开头的标记。AA AA应拒绝此项或AA应返回编辑应拒绝,因为它包含以下划线开头的标记。为什么它匹配
AB C
?更新的模式:(?!^)(?!.\s\u\w)^.*$works。谢谢你的努力+1.为什么它与
A B\u C
匹配?更新的模式:(?!^))(?!.\s\uw)^.*$有效。谢谢你的努力+我同意。