簡體   English   中英

我的Regex有什么問題?

[英]What am I doing wrong with my Regex?

我不確定自己在做什么錯。 我正在嘗試使用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標簽。

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

首先在我的代碼中,將關鍵字meta標簽替換為

<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**
    -->

謝謝

你在做什么錯 您正在使用正則表達式解析HTML

推薦的.NET庫: HTML Agility Pack

在沒有無用的生活教訓的情況下回答您的問題,您會因為貪婪的量詞而遇到麻煩。 嘗試通過添加問號使它們變得懶惰:

<meta\\s+?name=\"description\"\\s+?content=\"(?<Description>.*?)\"\\s*?/>

當然,此regex不適用於世界上所有頁面,但是如果您只需要為自己的模板制作一些快速替換腳本,則regex是最快,最簡單的解決方案和解決方案。

我同意@ serg555的回答-問題在於貪婪的量詞-使它們對'?'懶惰 應該解決問題

<meta\\s*name=\"description\"\\s*content=\"(?<Description>.*?)\"\\s*/>

學習,喜愛和使用DOM。 它是W3C(HTML標准團體)批准的解析XML(HTML是XML的子集)文檔的方式。 除非您有足夠的理由相信您輸入的HTML嚴重錯誤,否則通常這是最好的入門方法。

在這里學習

強烈建議您查看演練:從C#訪問DHTML DOM

您可能還需要嘗試使用jQuery,因為它使搜索DOM非常容易。 像這樣

我需要用C#代碼描述URL,並使用此站點檢查我的Regex代碼。

這是我最后的工作長官:

      WebClient x = new WebClient { Encoding = Encoding.UTF8 };
            string source = x.DownloadString(url);

            string description = Regex.Match(source, "<meta[^>]*name=[\"|\']description[\"|\'][^>]*content=[\"]([^\"]*)[\"][^>]*>", RegexOptions.IgnoreCase).Groups[1].Value;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM