Skip to main content
 首页 » 编程设计

c#之我的正则表达式做错了什么

2024年01月13日37shihaiming

我不确定我做错了什么。我正在尝试使用 asp.net regex.replace,但它一直在替换错误的项目。

我有 2 个替换。第一个做我想要的,它取代了我想要的。几乎是镜像的下一个替换并没有替换我想要的。

这是我的示例代码

<%@ Page Title="Tour" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
    <title>Website Portfolio Section - VisionWebCS</title> 
    <meta name="description" content="A" /> 
    <meta name="keywords" content="B" /> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <!-- **START** --> 

我希望替换这两个元标记。

<meta name=\"description\" content=\"A\" /> 
<meta name=\"keywords\" content=\"B\" /> 

在我的代码中,我首先将关键字元标记替换为

<meta name=\"keywords\" content=\"C\" /> 

这行得通,所以我的下一个任务是用这个替换描述元标记

<meta name=\"description\" content=\"D\" /> 

这不起作用,而是替换“关键字”元标记,然后替换“描述”标记。

这是我的测试程序,大家可以试试看。只需在 C# 控制台应用程序中通过它。

  private const string META_DESCRIPTION_REGEX = "<\\s* meta \\s* name=\"description\" \\s* content=\"(?<Description>.*)\" \\s* />"; 
        private const string META_KEYWORDS_REGEX = "<\\s* meta \\s* name=\"keywords\" \\s* content=\"(?<Keywords>.*)\" \\s* />"; 
        private static RegexOptions regexOptions = RegexOptions.IgnoreCase 
                                   | RegexOptions.Multiline 
                                   | RegexOptions.CultureInvariant 
                                   | RegexOptions.IgnorePatternWhitespace 
                                   | RegexOptions.Compiled; 
 
        static void Main(string[] args) 
        { 
 
            string text = "<%@ Page Title=\"Tour\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/Site.Master\" Inherits=\"System.Web.Mvc.ViewPage\" %><asp:Content ID=\"Content1\" ContentPlaceHolderID=\"HeadContent\" runat=\"server\">    <title>Website Portfolio Section - VisionWebCS</title>    <meta name=\"description\" content=\"A\" />    <meta name=\"keywords\" content=\"B\" /></asp:Content><asp:Content ID=\"Content2\" ContentPlaceHolderID=\"MainContent\" runat=\"server\"><!-- **START** -->"; 
            Regex regex = new Regex(META_KEYWORDS_REGEX, regexOptions); 
            string newKeywords = String.Format("<meta name=\"keywords\" content=\"{0}\" />", "C"); 
            string output = regex.Replace(text, newKeywords); 
 
            Regex regex2 = new Regex(META_DESCRIPTION_REGEX, regexOptions); 
            string newDescription = String.Format("<meta name=\"description\" content=\"{0}\" />", "D"); 
            string newOutput = regex2.Replace(output, newDescription); 
            Console.WriteLine(newOutput); 
        } 

这得到了我的最终输出

<%@ Page Title="Tour" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage" %> 
<asp:Content ID="Content1" ContentPlaceHold erID="HeadContent" runat="server"> 
    <title>Website Portfolio Section - VisionW 
        ebCS</title> 
    <meta name="description" content="D" /> 
</asp:Content> 
<asp:Conten t ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <!-- **START** 
    --> 

谢谢

请您参考如下方法:

你做错了什么?你是parsing HTML with a regex !

.NET 的推荐库:HTML Agility Pack