首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在powershell中,我需要拆分和提取Worldwide Web Publishing Service from string“The resouce publishing service is not available

在powershell中,我需要拆分和提取Worldwide Web Publishing Service from string“The resouce publishing service is not available
EN

Stack Overflow用户
提问于 2018-08-07 20:13:01
回答 2查看 28关注 0票数 0

在powershell中,我需要从字符串“资源全球发布服务不可用”中拆分和提取万维网发布服务。

我已经尝试过替换方法,但它不起作用

代码语言:javascript
复制
$message="The Resource World Wide Publishing Service is not available"
$newmessage=($message.Replace("The Resource is not available","")).ToString()
Write-Host $newmessage

但是输出$newmessage仍然是资源不可用

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-07 20:49:15

这里发生的情况是搜索部分与目标字符串不匹配。String.Replace()需要一个文字匹配。

在示例中,没有The Resource is not available这样的字符串。是的,单词在那里,但World Wide Publishing Service在这两个单词之间,所以不匹配。

作为解决方案,执行多个替换操作或使用正则表达式。简单的替换是这样的,

代码语言:javascript
复制
$message.Replace("The Resource ","").Replace(" is not available", "")
World Wide Publishing Service
票数 0
EN

Stack Overflow用户

发布于 2018-08-07 20:54:29

除了@vonPryz之外,您还可以使用前面提到的RegEx:

代码语言:javascript
复制
$message='The Resource World Wide Publishing Service is not available'
$match = [Regex]::Match($message, 'The Resource (.*) is not available')
if ($match.Success) {
    Write-Host $match.Groups[1].Value
} else {
    Write-Host 'Invalid input'
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51726529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档