【问题标题】:How is the regex split different or distinct from string split? [duplicate]正则表达式拆分与字符串拆分有何不同或不同? [复制]
【发布时间】: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_splitdocs.microsoft.com/en-us/powershell/module/…
  • @Cpt.Whale 我知道,因为 OP 的最后一个问题“操作员的帮助文件和从 REPL 控制台访问的功能如何 i>”,我不确定如何回答,但如果 OP 认为我可以发表我的评论作为答案,我会这样做。
  • 我添加了帮助文件链接,但是,是的@SantiagoSquarzon,问题是这些东西是如何区分的。
  • 我希望this answer链接的重复项回答所有问题。

标签: arrays string function powershell operators


【解决方案1】:

帮助文件说:

备注

Regex.Split 方法类似于 String.Split(Char[]) 方法,除了 Regex.Split 在分隔符处拆分字符串 由正则表达式而不是一组字符确定。

这一切都很好。

欢迎更深入的回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    相关资源
    最近更新 更多