正则表达式匹配 - 输出文件中的任何匹配字符

时间:2012-01-30 14:37:58

标签: c# .net windows

我正在尝试使用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 个答案:

答案 0 :(得分: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());
}