我在一个简单的报告脚本中很吃力。例如
$report.FullFormattedMessage = "This is the deployment test for Servername for Stackoverflow in datacenter onTV"
$report.FullFormattedMessage.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
现在我想挑选一些确定的单词,比如。。。
$dc = should be the 'onTV'
$srv = should be the 'Servername'
$srv = $report.FullFormattedMessage.contains... or -match ?? something like this?
.split()[]
的技巧对我不起作用,因为$report
有时看起来不一样。我怎么能那样做?
哦,好吧,我找到了一个解决方案,我不知道这是否是最好的。。。但我给你看:
$lines = $report.FullFormattedMessage.split()
$dc= ForEach ($line in $lines){
$line | Where-Object {$_ -match "onTV*"}
}
$srv= ForEach ($line in $lines){
$line | Where-Object {$_ -match "Server*"}
}
我可能会做一些类似的事情
$report.FullFormattedMessage = "This is the deployment test for Servername for Stackoverflow in datacenter onTV"
$dc = if ($report.FullFormattedMessage -match 'b(onTV)b') { $Matches[1] } # see details 1
$srv = if ($report.FullFormattedMessage -match 'b(Server[w]*)') { $Matches[1] } # see details 2
# $dc --> "onTV"
# $srv --> "Servername"
Regex详细信息1
b Assert position at a word boundary
( Match the regular expression below and capture its match into backreference number 1
onTV Match the characters “onTV” literally
)
b Assert position at a word boundary
Regex详细信息2
b Assert position at a word boundary
( Match the regular expression below and capture its match into backreference number 1
Server Match the characters “Server” literally
[w] Match a single character that is a “word character” (letters, digits, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)