내가 뭘 잘못하고 있는지 잘 모르겠다. 내가 asp.net regex.replace를 사용하려고하지만 그것은 잘못된 항목을 교체 유지합니다.Regex에서 내가 뭘 잘못하고 있니?
저는 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=\"description\" content=\"D\" />
과 설명 메타 태그를 대체하는이 작동
<meta name=\"keywords\" content=\"C\" />
와 키워드를 메타 태그를 교체 대신 "키워드"메타 태그를 대체 한 다음 "설명"태그를 대체합니다.
여기 내 테스트 프로그램이므로 모두 시도해 볼 수 있습니다. 바로 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**
-->
감사합니다 당신이 잘못하고 무엇
작동하지만 잘 모르겠습니다. 나는 욕심쟁이 한정어를 사용하고 있지만 "/>"을보고 멈출 때까지 계속 될 것이라고 생각했습니다. 그러면 왜 더 나아질까요? 이것이 얼마나 많은 표정을 지니고 있는지를 체크 할 때조차도 언제나처럼 돌아 왔습니다. – chobo2