如何使用正则表达式进行 2 次查找但只进行 1 次替换?



编辑:我现在正在使用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>

好的...你需要匹配它的多个位 - 然后用原始位替换大部分位,只用"title"匹配组替换一些位

这是有效的正则表达式(在记事本++中带有".匹配换行符" ON(

(Page Title = "([^"]*)")(.*)(<title>)([^<]*)(</title>)(.*)(<h1 id="page-title" class="font-36">)([^<]*)(</h1>)

因此,这给组提供了:

$1 (Page Title = "([^"]*)") - The first bit  
$2 ([^"]*) - INSIDE $1 - the thing we are wanting to use as replacements elsewhere  
$3 (.*) - everything up until....   
$4 (<title>)  
$5 ([^<]*) - inside the title tag (ie we want to replace this)  
$6 (</title>) - title closing tag  
$7 (.*) - everything up until...  
$8 (<h1 id="page-title" class="font-36">) - h1 opening tag  
$9 ([^<]*) - inside the h1 tag (ie we want to replace this)  
$10 (</h1>)

请注意使用否定字符组 - 因此$2匹配组表示任意数量的字符不是"这很重要,因为正则表达式是贪婪的(当我们点击该组的"时,我们希望停止,并移动到下一组(

所以我们的替代品是...

$1$3$4$2$6$7$8$2$10