【问题标题】:How can I use RegEx to do 2 finds but only 1 replace?如何使用 RegEx 进行 2 次查找但只有 1 次替换?
【发布时间】: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 = "([^"]*)")(.*)(&lt;title&gt;)([^&lt;])(&lt;/title&gt;)(.*)(&lt;h1 id="page-title" class="font-36"&gt;)([^&lt;]*)(&lt;/h1&gt;) 其中标题匹配组是 $2 替换为 $1$3$4$2$6$7$8$2$10 - 这并不完全有效,但也许你或其他人可以修复它?
  • 关于尝试parse XML with regex 而不是使用 DOM 解析器是徒劳的强制性链接。
  • @PoulBak,很清楚要使用什么输入是我的问题。但预期输出的一个具体示例是:&lt;h1 id="page-title" class="font-36"&gt;Member Access&lt;/h1&gt; 其中“成员访问”是通过搜索页面标题变量所包含的内容而找到的。每个页面上的变量都会不同。

标签: .net regex replace text-editor


【解决方案1】:

好的...您需要匹配它的多个位 - 然后将大部分位替换为原始位,仅将一些位替换为“标题”匹配组

这是有效的正则表达式(在 Notepad++ 中,“.matches newline”为 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

【讨论】:

  • 詹姆斯,谢谢!我认为这真的很接近。如果“页面标题=...”之前有文字,这也可以吗?我已经在 Notepad++ 中尝试过您的示例,但没有成功。
  • 詹姆斯,你能让它在这里工作吗?它实际上在您的记事本版本中有效吗? regex101.com/r/pNXIPO/1
  • @codewelldesign 我从来没有让它在正则表达式测试器中工作,但在记事本++ v7.6 中工作正常(见pasteboard.co/J8XDHlP.png)我知道它不是最新的,但如果正则表达式引擎,我会感到惊讶有什么不同... - 看起来仍然与您的屏幕截图相同,所以也许...
  • 詹姆斯,谢谢!我能够让您的代码在 regex101.com 上运行。我感谢您的帮助!这绝对有助于我前进。 regex101.com/r/KoBmHA/1
猜你喜欢
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多