【发布时间】:2020-05-18 11:30:22
【问题描述】:
编辑:我现在使用 PCRE RegEx 语言。
我有一个场景,我的网站上每个网页的顶部都有 VBScript 字符串值。 (此站点正在重新设计中。)我需要在搜索和替换场景中使用这些分配,使用 RegEx,并替换 HTML 元素的另一部分以赋予它该字符串值。
下面这个成功地从页面顶部的变量中提取了“成员访问”,我可以使用 $1 将该变量放置在某处。但这就是我卡住的地方。我需要将该值粘贴到其他地方,例如标签中。我在替换字段中输入什么来保留所有内容,但只替换某些项目,例如标题标签之间的文本?
我基本上需要找到两件事。查找第一个,然后在第二个查找上使用替换:<title>this text</title>
RegEx filter: /PageTitle = "(.*)"/ gm
Replacement string: <everything before Page Title string>PageTitle = "$1"<everything after PageTitle string><title>$1</title><Rest of content after title tag>
以下是我网站上每个页面的示例:
<%
Page Title = "Member Access"
MetaDescription = "This is a paragraph describing our website that we use to place into the meta description tag in the head. This will give information about our site."
Keywords = "Awesome, Cool, Rad, Tubular"
%>
<!doctype HTML>
<html dir="ltr" lang="en">
<head>
<meta charset="UTF-8">
<!-- Meta Tags -->
<meta name="description" content="This needs to be replaced with MetaDescription variable at top of page">
<meta name="keywords" content= "these, need, to, be, gone">
<meta name="viewport" content="width=device-width, initial-scale=1.0 shrink-to-fit=no">
<!-- Twitter and Facebook Social media tags -->
<meta property="fb:app_id" content="" />
<meta property="og:title" content="This needs to be replace with Page Title variable at top of page" >
<meta property="og:description" content="This needs to be replaced with MetaDescription variable at top of page">
<!-- Page Title -->
<title>This needs to be replaced with Page Title variable at top of page</title>
</head>
<body>
<div id="main" class="main-content">
<section class="inner-header divider layer-overlay overlay-dark-4" data-bg-img="/images/_interior-banners/THIS NEEDS TO BE REPLACED CONDITIONALLY BASED ON SITE FOLDER" style="background-image: url('/images/_interior-banners/THIS NEEDS TO BE REPLACED CONDITIONALLY BASED ON SITE FOLDER'); ">
<h1 id="page-title" class="font-36">This needs to be replaced by Page Title variable at top of page</h1>
rest of webpage content......
</div>
</section>
</body>
</html>
【问题讨论】:
-
我很困惑。您不能显示输入和预期输出的具体示例(带替换)吗?
-
我猜你需要匹配多个部分 - 然后用“标题”组替换其中的一些。 EG
(Page Title = "([^"]*)")(.*)(<title>)([^<])(</title>)(.*)(<h1 id="page-title" class="font-36">)([^<]*)(</h1>)其中标题匹配组是 $2 替换为 $1$3$4$2$6$7$8$2$10 - 这并不完全有效,但也许你或其他人可以修复它? -
关于尝试parse XML with regex 而不是使用 DOM 解析器是徒劳的强制性链接。
-
@PoulBak,很清楚要使用什么输入是我的问题。但预期输出的一个具体示例是:
<h1 id="page-title" class="font-36">Member Access</h1>其中“成员访问”是通过搜索页面标题变量所包含的内容而找到的。每个页面上的变量都会不同。
标签: .net regex replace text-editor