【发布时间】:2021-09-27 09:20:08
【问题描述】:
在usingsplit 运算符中,如:
**********************
PowerShell transcript start
Start time: 20210719105630
Username: gondor\nicholas
RunAs User: gondor\nicholas
Configuration Name:
Machine: gondor (Unix 5.8.0.59)
Host Application: /snap/powershell/160/opt/powershell/pwsh.dll
Process ID: 16273
PSVersion: 7.1.3
PSEdition: Core
GitCommitId: 7.1.3
OS: Linux 5.8.0-59-generic #66-Ubuntu SMP Thu Jun 17 00:46:01 UTC 2021
Platform: Unix
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0, 6.2.0, 7.0.0, 7.1.3
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
WSManStackVersion: 3.0
**********************
Transcript started, output file is strings.txt
PS /home/nicholas/power_shell> $string = "b1i9r8t7h"
PS /home/nicholas/power_shell> $array = $string -split "\d"
PS /home/nicholas/power_shell> $array
b
i
r
t
h
PS /home/nicholas/power_shell> $array = $string -split "\D"
PS /home/nicholas/power_shell> $array
1
9
8
7
PS /home/nicholas/power_shell> $array = $string.split("\D")
PS /home/nicholas/power_shell> $array
b1i9r8t7h
PS /home/nicholas/power_shell> $array = $string.split("\d")
PS /home/nicholas/power_shell> $array
b1i9r8t7h
PS /home/nicholas/power_shell> Stop-Transcript
**********************
PowerShell transcript end
End time: 20210719105811
**********************
使用上面的点运算符的结果是什么?它有什么作用吗?没有?似乎它应该做点什么或返回一个错误。
函数与运算符如何区分?
另见:
https://www.sqlshack.com/powershell-split-a-string-into-an-array/
https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.split?view=net-5.0
顺便说一句,操作员的帮助文件和从REPL 控制台访问的功能如何?
【问题讨论】:
-
.Split(..)字符串方法将在分隔符上拆分,它不适用于正则表达式,例如\D或\d。另一方面,-split运算符将允许您使用regex进行拆分,类似于[regex]::Split(..)。[regex]::Split('b1i9r8t7h','\D')和'b1i9r8t7h' -split '\D'会给你你期望的结果。 -
@SantiagoSquarzon 这应该是一个答案。要了解有关运营商的更多信息,请查看
about_split:docs.microsoft.com/en-us/powershell/module/… -
@Cpt.Whale 我知道,因为 OP 的最后一个问题“操作员的帮助文件和从 REPL 控制台访问的功能如何 i>”,我不确定如何回答,但如果 OP 认为我可以发表我的评论作为答案,我会这样做。
-
我添加了帮助文件链接,但是,是的@SantiagoSquarzon,问题是这些东西是如何区分的。
-
我希望this answer链接的重复项回答所有问题。
标签: arrays string function powershell operators