我在做什么毛病我的正则表达式? [英] What am I doing wrong with my Regex?

查看:184
本文介绍了我在做什么毛病我的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我做错了。我想使用asp.net regex.replace但它一直更换了错误的项目。

I am not sure what I am doing wrong. I am trying to use the asp.net regex.replace but it keeps replacing the wrong item.

我有2个替换。第一个做什么,我希望它它取代我想要什么。接下来的替代,这几乎是一个镜像不会取代我想要的。

I have 2 replaces. The first one does what I want it to it replaces what I want. The next replace that is almost a mirror image does not replace what I want.

所以这是我的样本code

So this is my sample code

<%@ 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标签。

I am looking to replace both the meta tags.

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

在我的code首先我用

In my code first I replace the keywords meta tag with

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

本作品,所以我的下​​一个任务是,以取代与此描述meta标签

This works so my next task is to replace the description meta tag with this

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

这不工作,而不是它所取代的关键词元标记,然后替换说明标签。

This does not work instead it replaces the "keywords" meta tag and then replaces the "description" tag.

下面是我的测试程序,所以大家可以尝试一下。只是通过它在C#控制台应用程序。

Here is my test program so you all can try it out. Just through it in 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);
        }

这让我的最终输出

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

感谢

推荐答案

要回答没有无用的生命吸取你的问题,你有因为贪婪量词的烦恼。尝试通过添加问号使他们懒惰的:

To answer your question without useless life lessons, you are having troubles because of greedy quantifiers. Try making them lazy by adding question marks:

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

当然这个正则表达式将不会在世界上的所有网页的工作,但如果你只需要尽自己的模板,一些快速更换的脚本,正则表达式是最快和最简单的解决方案,要走的路。

Sure this regex won't work for all pages in the world, but if you need just make some quick replacement script for your own templates, regex is the fastest and easiest solution and the way to go.

这篇关于我在做什么毛病我的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆