我的正则表达式出了什么问题?

cho*_*bo2 1 .net c# regex .net-3.5

我不确定我做错了什么.我试图使用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** -->
Run Code Online (Sandbox Code Playgroud)

我想要替换两个元标记.

<meta name=\"description\" content=\"A\" />
<meta name=\"keywords\" content=\"B\" />
Run Code Online (Sandbox Code Playgroud)

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

<meta name=\"keywords\" content=\"C\" />
Run Code Online (Sandbox Code Playgroud)

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

<meta name=\"description\" content=\"D\" />
Run Code Online (Sandbox Code Playgroud)

这不起作用,而是替换"keywords"元标记,然后替换"description"标记.

这是我的测试程序,所以你们都可以尝试一下.在C#console app中通过它.

  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);
        }
Run Code Online (Sandbox Code Playgroud)

这让我得到了最终的输出

<%@ 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**
    -->
Run Code Online (Sandbox Code Playgroud)

谢谢

Wil*_*ill 7

你做错了什么?您正在使用正则表达式解析HTML!

推荐的.NET库:HTML Agility Pack


ser*_*erg 6

为了回答你的问题而没有无用的生活课程,你因为贪婪的量词而遇到了麻烦.尝试通过添加问号使它们变得懒惰:

<meta\\s+?name=\"description\"\\s+?content=\"(?<Description>.*?)\"\\s*?/>
Run Code Online (Sandbox Code Playgroud)

当然这个正则表达式不适用于世界上所有页面,但如果您只需要为自己的模板制作一些快速替换脚本,那么正则表达式是最快速,最简单的解决方案.