[玩转系统] 周五的 REST、正则表达式和替换的乐趣
作者:精品下载站 日期:2024-12-14 07:41:00 浏览:12 分类:玩电脑
周五的 REST、正则表达式和替换的乐趣
首先,我需要一些可以使用的东西。
$feed = Invoke-RestMethod -Uri http://powershell.org/wp/feed/
这是我得到的返回结果的示例:
title : PowerShell Tip from the Head Coach of the 2014 Winter Scripting Games: Design for Performance and Efficiency!
link : http://powershell.org/wp/2014/01/23/powershell-tip-from-the-head-coach-of-the-2014-winter-scripting-games-design-fo
r-performance-and-efficiency/
comments : {http://powershell.org/wp/2014/01/23/powershell-tip-from-the-head-coach-of-the-2014-winter-scripting-games-design-f
or-performance-and-efficiency/#comments, 0}
pubDate : Thu, 23 Jan 2014 14:28:33 +0000
creator : creator
category : {category, category, category, category...}
guid : guid
description : description
encoded : encoded
commentRss : http://powershell.org/wp/2014/01/23/powershell-tip-from-the-head-coach-of-the-2014-winter-scripting-games-design-fo
r-performance-and-efficiency/feed/
cmdlet Invoke-RestMethod 已继续创建 XML 元素。我不需要做任何事情。接下来,我想重新格式化结果并使其美观。例如, pubDate 虽然可读,但不会作为真正的 [datetime] 排序,因为它被表示为字符串。有些属性(例如“描述”)被隐藏得更远。
PS C:\> $feed[1].description
#cdata-section
--------------
There are several concepts that come to mind when discussing the topic of designing your PowerShell commands for performance a...
我可以从这样的事情开始:
$feed | Select Title,Link,
@{Name="Description";Expression={$_.description.InnerText}},
@{Name="Published";Expression={$_.PubDate -as [datetime]}}
我获取了描述文本并将 pubDate 视为 [datetime] 对象。
更详细地查看此内容时,我首先看到的一件事是描述中充满了 HTML 代码。
$feed | Select Title,Link,
@{Name="Description";Expression={$_.description.InnerText}},
@{Name="Published";Expression={$_.PubDate -as [datetime]}}</pre>
<p>I’ve grabbed the description text and treated the pubDate as a [datetime] object.</p>
<p><a href="https://jdhitsolutions.com/blog/wp-content/uploads/2014/01/convertingxml1.png"><img fetchpriority="high" decoding="async" src="https://jdhitsolutions.com/blog/wp-content/uploads/2014/01/convertingxml1-1024x513.png" width="474" height="237" class="aligncenter size-large wp-image-3630" srcset="https://jdhitsolutions.com/blog/wp-content/uploads/2014/01/convertingxml1-1024x513.png 1024w, https://jdhitsolutions.com/blog/wp-content/uploads/2014/01/convertingxml1-300x150.png 300w, https://jdhitsolutions.com/blog/wp-content/uploads/2014/01/convertingxml1.png 1299w" sizes="(max-width: 474px) 100vw, 474px" /></a></p>
<p>One of the first things I see looking at this in more detail is that the description is full of HTML code.</p>
<pre class="lang:batch decode:true ">
title : Scripting Games Winter 2014 – Team Discussion Tips
link : http://powershell.org/wp/2014/01/06/scripting-games-winter-2014-team-discussion-tips/
Description : When you’re logged into the Games, you’ll notice that clicking on your team pulls up a “team
discussion” box. That’s a shared discussion area for you and your team. However, if you click on one
of the files you’ve uploaded, you’ll see the discussion turn into a “File Discussion.” We
retain a separate thread for<span class="continue-reading">... <a href="http://powershell.org/wp/2014/01/06/scripting-games-winter-2014-team-discussion-tips/">Continue Reading
»</a></span><div class="yarpp-related-rss">
<h3>Related posts:</h3><ol>
<li><a href="http://powershell.org/wp/2013/12/19/2014-winter-scripting-games-team-formation-tips/" rel="bookmark" >2014 Winter Scripting Games: Team Formation Tips</a></li>
<li><a href="http://powershell.org/wp/2014/01/02/registration-and-team-formation-now-available-for-the-scripting-ga
mes-2014-winter/" rel="bookmark" title="Registration and Team Formation NOW AVAILABLE for The Scripting Games
– 2014 Winter">Registration and Team Formation NOW AVAILABLE for The Scripting Games – 2014
Winter</a></li>
<li><a href="http://powershell.org/wp/2013/10/02/seeking-coaches-and-judges-for-the-winter-scripting-games/" rel="bookmark" >Seeking Coaches and Judges for
the Winter Scripting Games</a></li>
</ol>
</div>
Published : 1/6/2014 10:42:52 AM
为了使其更易于阅读,我想去掉 HTML 标签并将“-”之类的内容转换为我可以理解的内容。这就是正则表达式发挥作用的地方。
通过一些在线研究,我想出了一个正则表达式模式来查找 HTML 标签: 我可以像这样使用它:
PS C:\> [regex]$rgx = ""
PS C:\> $rgx.replace($feed[1].description.innertext,"")
There are several concepts that come to mind when discussing the topic of designing your PowerShell commands for performance and
efficiency, but in my opinion one of the items at the top of the list is “Filtering Left” which is what I’ll be
covering in this blog article. First, let’s start out by taking a... Continue Reading »
Related posts:
Winter Scripting Games 2014 Tip #1: Avoid the aliases
Winter Scripting Games 2014 Tip #2: Use #Requires to let PowerShell do the work for you
Winter Scripting Games 2014
Replace() 方法获取所有匹配项并将它们替换为“”,从而有效地将它们从文本中删除。因为,我有许多可能需要替换的项目,所以我定义了一个哈希表。
$decode=@{
''= ""
'’' = "'"
'“' = '"'
'”' = '"'
'»' = "..."
'–' = "--"
'–' = "@"
' ' = " "
}
然后在我的 Select-Object 语句中我可以重新格式化描述文本。
$feed | Select Title,
@{Name="Description";Expression={
$text = $_.Description.InnerText
#strip out html codes
foreach ($key in $decode.keys) {
[regex]$rgx=$key
$text = $rgx.Replace($text,$decode.Item($key)).Trim()
}
#use the cleaned up text
$text
}},
PowerShell 遍历哈希表键并将文本替换为键值。最终结果是 $text 现在是干净的。这是我的最终代码
$feed = Invoke-RestMethod -Uri http://powershell.org/wp/feed/
#hash table of HTML codes
#http://www.ascii.cl/htmlcodes.htm
$decode=@{
''= ""
'’' = "'"
'“' = '"'
'”' = '"'
'»' = "..."
'–' = "--"
'–' = "@"
' ' = " "
}
$feed | Select @{Name="Title";Expression={$_.title}},
@{Name="Description";Expression={
$text = $_.Description.InnerText
#strip out html codes
foreach ($key in $decode.keys) {
[regex]$rgx=$key
$text = $rgx.Replace($text,$decode.Item($key)).Trim()
}
#use the cleaned up text
$text
}},
@{Name="Published";Expression={$_.PubDate -as [datetime]}},
@{Name="Link";Expression={$_.Link}},
@{Name="Category";Expression={$_.Category.innertext -join ","}}
我所做的另一个添加是从 Category XML 元素获取文本,并将字符串数组连接到以逗号分隔的单行中。这是最终结果:
但等等,还有更多!我已获取 Invoke-RestMethod 的输出并将新对象写入管道。我可以做的一件事是将结果通过管道传输到 Out-GridView 并将其用作对象选择器。
c:\scripts\get-powershellorg.ps1 |
out-gridview -Title "Select one or more stories" -PassThru |
foreach { start $_.link }
我可以将结果发送到 Out-Gridview。从那里我可以选择一个或多个项目,单击“确定”并在我的网络浏览器中打开该项目。或者这个怎么样:
c:\scripts\get-powershellorg.ps1 |
Select Title,Description,Published,
@{Name="Link";Expression={ "$($_.link) "}},
Category |
ConvertTo-HTML -Title "PowerShell.org" | Out-string |
foreach { $_.Replace("<","") } |
out-file c:\work\psorg.htm -Encoding ascii
这里我正在进行一些替换。运行脚本后,我重新选择属性并自定义 Link 属性,将其变成 HTML 锚链接。我这样做是为了当我转换为 HTML 时我会得到一个带有可点击链接的表格。嗯,差不多了。您会看到,当 ConvertTo-HTML 获取 Link 属性的文本时,它会将 转回带引号的 HTML。这意味着我需要在将结果保存到文件之前将其转回 中。请注意,我正在利用 ForEach 脚本块中的管道。
foreach { $_.Replace("<","") }
Replace() 方法在替换 。最终结果是用一个命令进行两次替换。
我希望你能从中获得一些乐趣,也许还能学到一两个技巧。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag