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

C#-使用正则表达式的输入字符串的多值,c#,regex,C#,Regex,我有一个输入文本: 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 + "\

我有一个输入文本:

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;

您只需使用
\
转义正则表达式可选字符
\
,因为它是其语法的一部分:

Match m=Regex.Match(默认输入,“名称:(.*?\ \ \ \ \ |家族名称:(.*?\ \ \ \ |”);

代码也可以工作。

您是否仅限于正则表达式?很遗憾,是的!我需要学习将其用于一些家庭作业: