简体   繁体   English

C#RegEx匹配表达式中的可选元素

[英]An optional element in C# RegEx matching expression

How to make 'test2' an optional element in the following C# RegEx expression getting 'test1' value parsed out correctly when 'test2' element is missing? 当缺少“ test2”元素时,如何使以下C#RegEx表达式中的“ test2”成为可选元素,从而正确解析“ test1”值?

 StringBuilder sb = new StringBuilder();
 sb.AppendLine("    test1=123 any text in between  ");
 sb.AppendLine(" some ");
 sb.AppendLine(" more ");
 sb.AppendLine(" text in between ");
 sb.AppendLine("    test2=456   ");
 sb.AppendLine("    test1=789  some text .. test2=012   ");

 Regex regex = new Regex(@"test1=(?<test1>(\d+))((.|\s)+?)(test2=(?<test2>(\d+)))");

 MatchCollection matches = regex.Matches(sb.ToString());
 foreach (Match match in matches)
 {
     Group test1 = match.Groups["test1"];
     Group test2 = match.Groups["test2"];                
     System.Console.WriteLine("Test1 = {0}, Test2 = {1}", test1.Value, test2.Value);
 }

Thank you. 谢谢。


@Oded - I reply here as I can't get comment formatted properly and as my reply is longer than allowed by StackOverflow comment text length: @Oded-我在这里回复,因为我无法正确设置注释的格式,并且我的回复时间长于StackOverflow注释文本的长度:


Thank you. 谢谢。 Proposed in your second reply RegEx expression results in the following output: 在您的第二个回复中建议RegEx表达式将导致以下输出:

 Test1 = 123, Test2 = 
 Test1 = 789, Test2 =

It's not quite correct. 这不是很正确。 And your first reply RegEx expression results in 您的第一个回复RegEx表达式会导致

 Test1 = 123, Test2 = 456
 Test1 = 789, Test2 = 012

test output. 测试输出。 That's is correct. 没错

But if I change 但是如果我改变

sb.AppendLine("    test1=789  some text .. test2=012   ");

to

sb.AppendLine("    test1=789  some text .. test52=012   ");

then the test result output will have just one line 那么测试结果输出将只有一行

Test1 = 123, Test2 = 456

and I wanted it to be 我希望它成为

 Test1 = 123, Test2 = 456
 Test1 = 789, Test2 =

in that case. 在这种情况下。

Qualify that the whole test2 group is optional: 确认整个test2组是可选的:

@"test1=(?<test1>(\d+))((.|\s)+?)(test2=(?<test2>(\d+)))?"

From MSDN - Regular Expression Language - Quick Reference : 从MSDN- 正则表达式语言-快速参考

? - Matches the previous element zero or one time. -匹配上一个元素零或一次。

add a ? 添加一个? after the element you want to be optional 在您想要成为可选元素之后

.|\\s can be replaced by . .|\\s可以替换为. since . 从此. matches whitespace as well 也匹配空白

To match newlines as well you have to pass Singleline option Regex regex = new Regex(@"test1=(?<test1>(\\d+))((.)+?)(test2=(?<test2>(\\d+)))?",RegexOptions.Singleline); 要匹配换行符,还必须传递单行选项Regex regex = new Regex(@"test1=(?<test1>(\\d+))((.)+?)(test2=(?<test2>(\\d+)))?",RegexOptions.Singleline);

(The solution by Oded is does all this) (Oded的解决方案就是完成所有这一切)

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

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