2012-01-30 55 views
1

我想使用Regex.Match方法查找文件中的匹配字符。正则表达式匹配方法使用内存中文件的行(strLine),并根据指定的(m_strRegEx)和任何适用的选项进行检查。虽然我怎么才能输出这个只是数学角色?正则表达式匹配 - 输出文件中的任何匹配字符

Match mtch; 
if (m_bIgnoreCase == true) 
    mtch = Regex.Match(strLine, m_strRegEx, RegexOptions.IgnoreCase); 
else 
    mtch = Regex.Match(strLine, m_strRegEx); 

回答

1

我想你需要以下条件:

Match mtch = Regex.Match(strLine, m_strRegEx, m_bIgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); 
if (mtch.Success) 
{ 
    Console.WriteLine(mtch.Value); 
} 

,或者你可以在一个去搜索strLine中的所有事件:

MatchCollection matches = Regex.Matches(strLine, m_strRegEx, m_bIgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); 
foreach (var match in matches) 
{ 
    Console.WriteLine(match.ToString()); 
}