PowerShell 正则表达式如何处理多行字符串? [英] How does PowerShell regex work with multi-line strings?

查看:90
本文介绍了PowerShell 正则表达式如何处理多行字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这让我发疯了,因为我的正则表达式正在使用 Rubular,但 PowerShell 没有按我预期的那样工作.

  1. 我在网络目录上做了一个 Get-ChildItem,然后将输出定向到一个 txt 文件中.
  2. 我从如下所示的文本文件中删除了目录信息:

  1. 当我使用 PowerShell 尝试编写正则表达式以删除目录信息时,我遇到了一些问题.

当我使用时:

$var = Get-Contnet文件路径"$var -match "目录.*"

PowerShell 抓取了我要查找的文本,但它没有抓取以新行开头的文本,我得到:

目录:\\Drive\Unit\Proposals\Names\Location\crazy 文件夹路径\even crazier 文件夹路径\unbelievable 文件夹路径\

所以...当我使用:

$var -match "Directory.*\n.*"

我一无所获...<​​/p>

当我在 Rublar 上尝试这个时它工作正常,我在这里遗漏了什么?任何帮助都会很棒,谢谢!

解决方案

Filburt 的回答很好,看起来正则表达式并不是这里使用的最佳工具.但是,您遇到了一个可能会再次引起混乱的问题.这里的问题是您使用 Get-Content 填充的变量不是多行字符串.它是一个字符串数组:

$var = Get-Content "文件路径"$var.GetType() # 显示 'Object[]'

当您对 $var 运行正则表达式匹配时,它会单独匹配数组中的每个对象(文件中的每一行).它不能匹配超过一行的末尾,因为下一行是一个新对象.

这里的一种解决方法是将字符串数组压平为一个字符串,如下所示:

$var = (Get-Content "文件路径" | Out-String)$var.GetType() # 现在显示 'String'

在 Powershell 中,有时很难区分您是在处理单个 String 对象还是一个 String 数组.如果将它们输出到控制台,它们看起来是相同的.在这些情况下,GetType()Out-String 可能是有用的工具.

编辑:从 Powershell 3.0 开始,Filesystem 提供程序包括用于 Get-Content-Raw 开关.该开关指示 Get-Content 一次读取文件而不将其拆分为块.它比使用 Out-String 解决方法要快得多,因为它不会浪费时间将各个部分分开,然后再将它们重新组合在一起.

Alright, this is driving me nuts because my regex is working on Rubular, but PowerShell is not working as I expect.

  1. I did a Get-ChildItem on a network directory and then directed the output into a txt file.
  2. I went to remove the directory info from the text file that appears like the following:

  1. When I use PowerShell to try and write a regex to remove the Directory info, I run into some problems.

When I use:

$var = Get-Contnet "file path"
$var -match "Directory.*"

PowerShell grabs the text I am looking for, BUT it doesn't grab the text that starts on a new line, I get:

Directory: \\Drive\Unit\Proposals\Names\Location\crazy folder path\even crazier folder path\unbelievable folder path\

So... when I use:

$var -match "Directory.*\n.*"

I get nothing...

When I try this on Rublar it works fine, what am I missing here? Any help would be great, thanks!

解决方案

Filburt's answer is a good one, and it doesn't look like regular expressions are the best tool to use here. However, you bumped into an issue that may cause confusion again down the road. The issue here is that the variable you populated with Get-Content is not a multi-line string. It is an array of strings:

$var = Get-Content "file path"
$var.GetType() # Shows 'Object[]'

When you run a regex match against $var, it matches against each object in the array (each line in the file) individually. It can't match past the end of a line because the next line is a new object.

One workaround here is to flatten that array of strings down into a single string like this:

$var = (Get-Content "file path" | Out-String)
$var.GetType() # Shows 'String' now

In Powershell it can sometimes be tricky to tell when you're dealing with a single String object versus an array of Strings. If you output them to the console they appear identical. In those cases, GetType() and Out-String can be useful tools.

Edit: As of Powershell 3.0, the Filesystem provider includes a -Raw switch for Get-Content. That switch instructs Get-Content to read the file all at once without splitting it into chunks. It is significantly quicker than using the Out-String workaround, because it doesn't waste time pulling pieces apart only to put them back together again.

这篇关于PowerShell 正则表达式如何处理多行字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆