仅当存在现有匹配项时,才返回 true,但这不是第一个匹配项

本文关键字:true 返回 这不是 第一个 存在 | 更新日期: 2023-09-27 17:57:04

我想我已经在标题中写了我想做的事情,所以现在切中要害:

  1. 我有一个带有 url 链接的.txt文件,它们的源代码将由正则表达式解析。
  2. 每个链接的源代码都被这样抓取:

    public static string getSourceCode(string url)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader(resp.GetResponseStream());
        string sourceCode = sr.ReadToEnd();
        sr.Close();
        resp.Close();
        return sourceCode;
    }
    

每个源代码都包含以下文本:

..code..
..code..
    <p class="content">
                                exampleexampleexample
                                        </p>
..code..
..code..
    <p class="content">
                                example
                                        </p>
..code..
..code..

content元素的元素更多。

  1. 我通过以下方式获得content内容:

Regex k = new Regex(@"<p class=""question-content"">['r'n's]*('S.*)"); var g = k.Matches(sourceCode);

现在我可以轻松提取每个匹配项:

g[1].ToString() <-- first match
g[2].ToString() <-- second match
g[3].ToString() <-- thirdmatch

等。

但是我想做的是提取这些链接,其中:第一场比赛不包含XYZ,但至少在其他比赛中有XYZ

例如:

第一个

链接的源代码包含第一个和第三个匹配

第二个链接的源代码仅在第一个匹配

第三个链接的源代码只包含第三个匹配

溶液

我从中得到每场比赛的同事:

MatchCollection b1 = Regex.Matches(sourceCode, @"<p class=""content"">['r'n's]*('S.*)");

我接下来要做的是通过以下方式检查第一个匹配项是否不包含"示例":

if (!b1[0].ToString().Contains("example"))

并检查此函数的结果:

bool checkAnother(int amount, MatchCollection m)
{     
    for (int i=1; i<=amount-1; i++)
    {
        if (m[i].ToString().Contains("example"))
            return true;
    }
    return false;
}

这就是代码:

            MatchCollection b1 = Regex.Matches(sourceCode, @"<p class=""content"">['r'n's]*('S.*)");
            if ((!b1[0].ToString().Contains("example")) && (checkAnother(b1.Count, b1)))
            {dataGridView1.Rows[i].Cells[2].Value = "GOOD";                   
            }

仅当存在现有匹配项时,才返回 true,但这不是第一个匹配项

您尝试执行的操作不适合正则表达式。

多行匹配、捕获组和环顾四周可能是可能的,但 IMO 不值得在一个无法维护的解决方案上投入大量精力。

尝试改为在后处理步骤中验证找到的匹配项。假设你像这样抓住比赛:

var g = k.Matches(sourceCode);

。您可以通过以下方法轻松实现:

var isFirstOk = !g[0].Value.Contains("XYZ");
var areAllOk = isFirstOk && g.Cast<Match>().Skip(1).Any(m => m.Value.Contains("XYZ"));