powershell - 如何在 Powershell 中输出正则表达式匹配中的所有字符串?
问题描述
我想在文件中搜索这个正则表达式:
<html:productType>.+?</html:productType>
如何在另一个文件中输出 .+ 的所有匹配项?上面的正则表达式?另外,您是否推荐任何其他工具/脚本语言来处理此类内容?
解决方案
由于没有关于数据结构的信息,无论是 XML 还是 HTML 或其他什么,Powershell 和 Regex 就足够了。我推荐正则表达式命名组来标记重要片段。请参阅以下演示:
#prepare demo data
@'
<node>
<html:productType>ABC</html:productType>
<html:productType>DEF</html:productType>
</node>
'@ | Out-File Demo.txt
cat Demo.txt | Select-String '<html:productType>(?<MyGroup>.+?)<\/html:productType>' | % {
$_.Matches[0].Groups['MyGroup'].Value
} | Out-File Demo.out
推荐阅读
- java - 如果优先级队列是最小堆的实现,那么优先级队列如何始终产生相同的结果
- android - 如何将资产文件夹中的 docx 文件存储到 android 设备上的设备公共目录,例如“下载”
- javascript - 在第二个选择框中禁用第一个选择框的选定选项
- nginx - nginx websocket握手错误400使用AWS ELB
- javascript - 基于旧版本代码开发npm包
- spring-boot - 停止使用 Stream 侦听器的消息
- web-scraping - BeautifulSoup 在使用 .find 方法时找不到所有标签?
- javascript - AJAX 表单提交不重新加载内容
- python - 多处理中的内置函数 ID 行为
- facebook - 登录失败您无法使用 facebook 登录此应用程序或网站,因为它的 facebook 登录实现存在问题(NuxtJs 应用程序)