.net - 如何使用 RegEx 进行 2 次查找但只有 1 次替换?
问题描述
编辑:我现在使用 PCRE RegEx 语言。
我有一个场景,我网站上每个网页的顶部都有 VBScript 字符串值。(此站点正在重新设计中。)我需要在搜索和替换场景中使用这些分配,使用 RegEx,并替换 HTML 元素的另一部分以赋予它该字符串值。
下面成功地从页面顶部的变量中提取“成员访问”,我可以使用 $1 将该变量放置在某处。但这就是我卡住的地方。我需要将该值粘贴到其他地方,例如标签中。我在替换字段中输入什么来保留所有内容,但只替换某些项目,例如标题标签之间的文本?
我基本上需要找到两件事。找到第一个,然后在第二个 find 上使用替换:
<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>
解决方案
好的...您需要匹配它的多个位 - 然后将大部分位替换为原始位,仅将一些位替换为“标题”匹配组
这是有效的正则表达式(在记事本++中,“。匹配换行符”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
匹配组意味着任意数量的字符不是 a"
这很重要,因为正则表达式是贪婪的(我们希望在我们"
为该组点击 a 时停止,并移动到下一个组)
所以我们的替代品是...
$1$3$4$2$6$7$8$2$10
推荐阅读
- c# - 运行多个存储过程的最佳方法是什么?
- javascript - 如何将多个返回的 SQL 行组合成一个字符串
- sql-server - SQL:如何将行转换为列(需要另一种方法)
- apache-spark - 将 Ceph 端点设置为 DNS 在 Hadoop 中不起作用
- javascript - 如果我有一些用花括号包裹的代码怎么办?
- node.js - 为什么await不在fs中等待
- python-3.x - 'CountVectorizer' 对象不可下标?
- reactjs - 类型无效——需要一个字符串但得到:未定义
- regex - eclipse中的多行搜索
- google-apps-script - 从谷歌表格发送电子邮件