下面的代码在正则表达式与字符串匹配时起作用。如果其中一个角色不在那里,例如MONEY-STAT丢失了?正则表达式缺失字符
string s = "MONEY-ID123456:MONEY-STAT43:MONEY-PAYetr-1232832938";
Regex regex =
new Regex(@"MONEY-ID(?<moneyId>.*?)\:MONEY-STAT(?<moneyStat>.*?)\:MONEY-PAYetr-(?<moneyPaetr>.*?)$");
Match match = regex.Match(s);
if (match.Success)
{
Console.WriteLine("Money ID: " + match.Groups["moneyId"].Value);
Console.WriteLine("Money Stat: " + match.Groups["moneyStat"].Value);
Console.WriteLine("Money Paetr: " + match.Groups["moneyPaetr"].Value);
}
Console.WriteLine("hit <enter>");
Console.ReadLine();
然后,它将不匹配并且不会'内执行的代码,如果(match.Success) {...}' – Qiau 2012-07-30 10:24:20
你为什么不自己尝试? – 2012-07-30 10:24:21
@Steve B不起作用,如果字符串不起作用。任何我可以改变正则表达式字符串? – Alvin 2012-07-30 10:26:22