2

这是我试过的代码

private void button2_Click(object sender, EventArgs e)
{

    extractEmail(richTextBox1.Text);            
    richTextBox2.Lines = emails.ToArray();                      
}

public void extractEmail(String htmlDoc)
{

    Regex exp = new Regex("^Call:(.*)", RegexOptions.IgnoreCase);
    MatchCollection matchCollection = exp.Matches(htmlDoc);
    foreach (Match m in matchCollection)
    {
        if (!emails.Contains(m.Value))
            emails.Add(m.Value);
    }
}

我尝试了很多替代方案,但它不起作用。我可以使用代码找到空行

"^(.*)"

但我无法提取以 Call 开头的行:

提前致谢。

编辑 - -

样本输入:

Call: (044) 43593164

asdfasdf

adsfadsf

Call: (044) 43593164
asdfadf

我得到的输出:

没有任何。没有错误没有输出。

编辑 - -

感谢 Nico Schertler 找到了答案

 Regex exp = new Regex("^Call:(.*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        MatchCollection matchCollection = exp.Matches(htmlDoc);
        foreach (Match m in matchCollection)
        {
            if (!emails.Contains(m.Value))
                emails.Add(m.Value);

        }
        richTextBox2.Lines = emails.ToArray();
4

1 回答 1

3

这个正则表达式将匹配所有以Call:

正则表达式:^Call:\s+.*

在此处输入图像描述

例子

示例文本

Call: (044) 43593164
asdfasdf
adsfadsf
Call: (044) 43593164
asdfadf

代码

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          Regex re = new Regex(@"^Call:\s+.*",RegexOptions.IgnoreCase | RegexOptions.Multiline);
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx=0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
              }
            mIdx++;
          }
        }
    }
}

火柴

[0] => Call: (044) 43593164
[1] => Call: (044) 43593164
于 2013-07-06T16:58:23.370 回答