【问题标题】:why regex split add to pattern \r\n为什么正则表达式拆分添加到模式 \r\n
【发布时间】:2023-04-01 07:31:01
【问题描述】:

我想用 html div 标签分割文章的正文,所以我有一个搜索 div 的模式。 问题是模式也分裂了 \r\n [在此处输入图片说明][1]

string pattern = @"<div[^<>]*>(.*?)</div>";
string[] bodyParagraphsnew = Regex.Split(body, pattern,RegexOptions.None);
Response.Write("num of paragraph =" + bodyParagraphsnew.Length);
for (int i = 0; i < bodyParagraphsnew.Length; i++)
{
    Response.Write("bodyParagraphs" + i + "= " + bodyParagraphsnew[i]+ Environment.NewLine);
}

当我调试这段代码时,我在数组 bodyParagraphsnew 中看到很多“\r\n”。

可以看出该模式包括由字符串“\r\n”分割 我尝试将 \r\n 替换为空字符串,我希望 bodyParagraphsnew 长度会改变。但不是。我得到的不是包含 \r\n 它包含“”的项目(在数组中) 为什么?

这里是图片http://i.stack.imgur.com/Hxqki.gif 的链接,用于解释问题

【问题讨论】:

  • 您能否向我们展示导致此问题的正文字符串示例?

标签: c# regex split


【解决方案1】:

您看到的是第一个

标记的结尾和下一个
标记的开头之间的文本。这就是 Split 所做的,它会在正则表达式匹配的 between 之间找到文本。

这里令人好奇的是,您还将获得打开和关闭标签之间的文本,因为您将括号放在字符串中,形成capturing group。考虑以下程序:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        string body = "<div>some text</div>\r\n<div>some more text</div>";

        string pattern = @"<div[^>]*?>(.*?)</div>";
        string[] bodyParagraphsnew = Regex.Split(body, pattern, RegexOptions.None);
        Console.WriteLine("num of paragraph =" + bodyParagraphsnew.Length);
        for (int i = 0; i < bodyParagraphsnew.Length; i++)
        {
            Console.WriteLine("bodyParagraphs {0}: '{1}'", i, bodyParagraphsnew[i]);
        }
    }
}

你会从中得到:

  1. "" - 取自第一个
    之前的空字符串。
  2. "some text" - 第一个
    的内容,因为捕获组。
  3. "\r\n" - 第一个
结尾和最后一个
开头之间的文本。
  • "some more text" - 第二个 div 的内容,同样是因为捕获组。
  • "" - 取自最后一个
  • 之后的空字符串。

    您可能想要的是 div 标签的内容。这可以有点使用以下代码来实现:

    using System;
    using System.Text.RegularExpressions;
    
    class Program
    {
        static void Main(string[] args)
        {
            string body = "<div>some text</div>\r\n<div>some more text</div>";
    
            string pattern = @"<div[^>]*?>(.*?)</div>";
            MatchCollection bodyParagraphsnew = Regex.Matches(body, pattern, RegexOptions.None);
            Console.WriteLine("num of paragraph =" + bodyParagraphsnew.Count);
            for (int i = 0; i < bodyParagraphsnew.Count; i++)
            {
                Console.WriteLine("bodyParagraphs {0}: '{1}'", i, bodyParagraphsnew[i].Groups[1].Value);
            }
        }
    }
    

    但请注意,在 HTML 中,div 标签可以相互嵌套。例如,以下是一个有效的 HTML 字符串:

    string test = "<div>Outer div<div>inner div</div>outer div again</div>";
    

    在这种情况下,正则表达式不起作用!这主要是由于 HTML 不是Regular Language。为了处理这种情况,您将需要编写一个解析器(其中正则表达式只是一小部分)。但是我个人不会打扰,因为已经有很多开源 HTML 解析器可用 HTML Agility Pack

    【讨论】:

      【解决方案2】:

      两种可能

      1. 您使用 llist 而不是数组和 list.remove
      2. 您通过数组搜索 \r\n 并按索引将其删除

        if(bodyParagraphsnew[i] == "\r\n")
        {
        bodyParagraphsnew = bodyParagraphsnew.Where(w => w != bodyParagraphsnew[i]).ToArray();
        }
        

      不是很好,但也许这就是你要找的东西

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签