C#-使用正则表达式的输入字符串的多值

问题描述 投票:0回答:1

我有输入文字:

string DefaultInput = "Name : John | FamilyName : Doe |";

我希望能够使用Regex从1个输入中提取“ John”和“ Doe”以及其他值

我的代码:

Match m = Regex.Match(DefaultInput,"Name : (.*?) | FamilyName : (.*?) |");
this.textBox1.Text = "ProfileName : " + m.Groups[1].Value + "\r\nProfileFamilyName : " + m.Groups[2].Value;
c# regex nsregularexpression
1个回答
0
投票

您只需要用|转义正则表达式替代字符\,因为它是其语法的一部分:

Match m = Regex.Match(DefaultInput, "Name : (.*?) \\| FamilyName : (.*?) \\|");

并且代码有效。

© www.soinside.com 2019 - 2024. All rights reserved.