Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何提取以单词“quot”开头的行;电话:“;使用正则表达式_C#_Regex - Fatal编程技术网

C# 如何提取以单词“quot”开头的行;电话:“;使用正则表达式

C# 如何提取以单词“quot”开头的行;电话:“;使用正则表达式,c#,regex,C#,Regex,这是我试过的代码 private void button2_Click(object sender, EventArgs e) { extractEmail(richTextBox1.Text); richTextBox2.Lines = emails.ToArray(); } public void extractEmail(String htmlDoc) { Regex exp = new Re

这是我试过的代码

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();

此正则表达式将匹配以
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++;
          }
        }
    }
}

你能提供一个例子吗?似乎你试图从html中提取,是帮助你做到这一点的工具。请解释你喜欢什么样的例子。我使用上述代码提取电子邮件、电话号码等,但我无法提取以“Call:”开头的整行代码。感谢您的时间。以及设置
RegexOptions.Multiline
所需的输入和输出示例。这将允许
^
字符匹配任何行的开头。
[0] => Call: (044) 43593164
[1] => Call: (044) 43593164