下面的代碼在正則表達式與字符串匹配時起作用。如果其中一個角色不在那裏,例如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