如何解决在字符串中搜索特定单词
我正在努力编写一个简单的报告脚本。 例如
$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
现在我想挑选一些 certians 的话,比如......
$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"
正则表达式详细信息 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
正则表达式详细信息 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)
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。