如何使用PowerShell和Regex创建批处理处理器来替换HTML标记(解析)| Neculai Fantanaru.
ro  fr  en  es  pt  ar  zh  hi  de  ru
ART 2.0 ART 3.0 ART 4.0 ART 5.0 ART 6.0 Pinterest

如何创建具有PowerShell和Regex的批处理处理器来替换HTML标记(解析)

On Iunie 16, 2021, in Leadership and Attitude, by Neculai Fantanaru

您可以在此处查看完整代码:HTTPS://帕萨特斌.com/X GN QJ QS7

将使用PowerShell代码修改的HTML页面的代码示例。 将上述文本复制到.html文件,将其保存到位置C:\ folder1

   

 xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="ro">

如何使用PowerShell和Regex创建批处理处理器来替换HTML标记(解析)|  Neculai Fantanaru.
 rel="canonical" href="https://MY-WEBSITE.COM" />
 name="description" content="I LOVE HTML and CSS"/>

 name="keywords" content="abordarea frontala a lucrurilor neelucidate"/>
 name="abstract" content="My laptop works just fine"/>
 name="Subject" content="I think I need a new car."/>
 property="og:url" content="https://otherwebsite.com"/>
 property="og:title" content="Nobody is here?" />
 property="og:description" content="Dance is my passion."/>





下面的PowerShell代码将通过解析数据将HTML标记的内容复制到其他标记。 您只需要填写标签</span>四<span class="tabela_shop_donate_5"><meta name =“描述”...... /></span></p> <!-- HTML generated using hilite.me --> <div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .4em;padding:.2em .6em;"> <pre style="margin: 0; line-height: 125%"><span style="color: #996633">$sourcedir</span> = <span style="background-color: #fff0f0">"C:\Folder1\"</span> <span style="color: #996633">$resultsdir</span> = <span style="background-color: #fff0f0">"C:\Folder1\"</span> <span style="color: #007020">Get-ChildItem</span> -Path <span style="color: #996633">$sourcedir</span> -Filter *.html | <span style="color: #008800; font-weight: bold">ForEach</span>-Object { <span style="color: #996633">$content</span> = <span style="color: #007020">Get-Content</span> -Path <span style="color: #996633">$_</span>.FullName -Raw <span style="color: #888888"># Copy the content of the tag <link rel="canonical" in the tag "OG:URL" and in the tag "@ID": #</span> <span style="color: #996633">$replaceValue</span> = (<span style="color: #007020">Select-String</span> -InputObject <span style="color: #996633">$content</span> -Pattern <span style="background-color: #fff0f0">'(?<=<link rel="canonical" href=").*(")'</span>).Matches.Value <span style="color: #996633">$content</span> = <span style="color: #996633">$content</span> <span style="color: #333333">-replace</span> <span style="background-color: #fff0f0">'(?<=<meta property="og:url" content=").*(")'</span>,<span style="color: #996633">$replaceValue</span> <span style="color: #996633">$content</span> = <span style="color: #996633">$content</span> <span style="color: #333333">-replace</span> <span style="background-color: #fff0f0">'(?<="@id": ").*(")'</span>,<span style="color: #996633">$replaceValue</span> <span style="color: #888888"># Copy the content of the tag <title> in the tags ABSTRACT, SUBJECT, OG:TITLE, HEADLINE, KEYWORDS #</span> <span style="color: #996633">$replaceValue</span> = (<span style="color: #007020">Select-String</span> -InputObject <span style="color: #996633">$content</span> -Pattern <span style="background-color: #fff0f0">'(?<=<title>如何使用PowerShell和Regex创建批处理处理器来替换HTML标记(解析)| Neculai Fantanaru.)').Matches.Value $content = $content -replace '(?<=,$replaceValue $content = $content -replace '(?<=,$replaceValue $content = $content -replace '(?<=,$replaceValue $content = $content -replace '(?<=,$replaceValue $content = $content -replace '(?<="headline": ").+(?=")',$replaceValue $content = $content -replace '(?<="keywords": "如何使用PowerShell和Regex创建批处理处理器来替换HTML标记(解析)| Neculai Fantanaru.", # Copy the content of the tag $replaceValue = (Select-String -InputObject $content -Pattern '(?<=).Matches.Value $content = $content -replace '(?<=,$replaceValue $content = $content -replace '(?<="description": "如何使用PowerShell和Regex创建批处理处理器来替换HTML标记(解析)| Neculai Fantanaru.", Set-Content -Path $resultsdir\$($_.name) $content }

可选的。 这是一个正则表达式表达式,它将在HTML页面中更改“关键字”标记,在每个单词后添加逗号。

使用Notepad ++ - > Ctr + F - >检查:正则表达式

SEARCH: (?s)<title>.*?<\/title>.*?<meta\x20name="keywords"\x20content="\K(\w+)|\G[^\w\r\n]+(\w+)  
REPLACE BY:  ?1\l\1:,\x20\l\2

That's all folks.

If you like my code, please SHARE IT

您还可以查看代码版本Python


Latest articles accessed by readers:

  1. An Eye To See And A Mind To Understand
  2. Turn Towards Me With An Eye Full Of Your Own Gaze
  3. The Snapshot Of Magic In God's Universe
  4. Rhythm Of My Heart

Donate via Paypal

Alternate Text

RECURRENT DONATION

Donate monthly to support
the NeculaiFantanaru.com project

SINGLE DONATION

Donate the desired amount to support
the NeculaiFantanaru.com project

Donate by Bank Transfer

Account Ron: RO34INGB0000999900448439

Open account at ING Bank

Join The Neculai Fantanaru Community



* Note: If you want to read all my articles in real time, please check the romanian version !

decoration
About | Site Map | Partners | Feedback | Terms & Conditions | Privacy | RSS Feeds
© Neculai Fântânaru - All rights reserved