Get*_*ock 2 regex parsing powershell-4.0
序幕
我正在尝试执行一项操作,该操作要求我解析特定文件中的每个单词。最直接的方法是使用以下命令加载文本:
$content = Get-Content -Path .\<filename>
Run Code Online (Sandbox Code Playgroud)
然后我会将每个单独的单词分成单独的行(这使我能够非常快速地进行单词计数和单个单词搜索)。问题是当我使用这行代码时:
$content.split("\s+")
Run Code Online (Sandbox Code Playgroud)
它应该在每个(一个或多个)空白字符上创建一个新行(分割)。不幸的是,我的结果如下所示:
$content.split("\s+")
The SpeechSynthe
izer cla
provide
acce
to the functionality of a
peech
ynthe
i
engine that i
in
talled on the ho
t computer. In
talled
peech
ynthe
i
engine
Run Code Online (Sandbox Code Playgroud)
但当我跑步时
$content -split("\s+")
Run Code Online (Sandbox Code Playgroud)
结果将正确显示:
$content -split("\s+")
The
SpeechSynthesizer
class
provides
access
to
the
functionality
of
a
speech
synthesis
Run Code Online (Sandbox Code Playgroud)
我的问题 使用 powershell V.4 我无法理解执行操作之间的区别。
$content.split("\s+")
和
$content -split("\s+")
Run Code Online (Sandbox Code Playgroud)
是。以及为什么他们输出不同的结果。
该功能刚刚被破坏了吗?
还有其他我不知道的差异吗?
该
-split运算符采用正则表达式,并且要分割任意数量的空格,您可以使用 regexp"\s+"。
和
要分割单个或多个字符,您还可以使用
System.String对象方法Split()。
PS C:\> 'a,b;c,d'.Split(',') -join ' | '
a | b;c | d
PS C:\> 'a,b;c,d'.Split(',;') -join ' | '
a | b | c | d
因此,您只需传递需要使用 分割的符号$content.split("\s+"),而不是传递匹配空格的正则表达式。
在$content -split("\s+"),中,是匹配1 个或多个空白符号的\s+正则表达式模式。
| 归档时间: |
|
| 查看次数: |
5092 次 |
| 最近记录: |