gpt4 book ai didi

powershell - Powershell在搜索字符串后返回下一个单词

转载 作者:行者123 更新时间:2023-12-03 00:42:26 25 4
gpt4 key购买 nike

我很抱歉,如果这是重复的,但我徒劳地搜索了。我正在尝试在Powershell中的搜索字符串之后返回(包括)作为继续进行的单词。

$wordtest="one two three four"

如何仅提取 包含在内的 two three

我已经徒劳地尝试了:
$wordtest.substring($wordtest.IndexOf('two') +1)


($wordtest.Split('two')[1])

我使用以下方法来工作 独家 three查询:
(($wordtest -split "two")[1].trim() -split ' ')[0]
three

谢谢。

最佳答案

您也可以为此使用正则表达式:

$wordtest   = "one two three four"
$searchWord = "two"
if ($wordtest -match "($searchWord\s+\w+)") {
$matches[0]
}

结果

two three



请注意,如果 $searchWord包含正则表达式的特殊字符,例如 .$,则需要先 [Regex]::Escape($searchWord)

正则表达式中的特殊字符
Char    Description                         Meaning
------- ----------------------------------- ---------------------------------------------------------
\ Backslash Used to escape a special character
^ Caret Beginning of a string
$ Dollar sign End of a string
. Period or dot Matches any single character
| Vertical bar or pipe symbol Matches previous OR next character/group
? Question mark Match zero or one of the previous
* Asterisk or star Match zero, one or more of the previous
+ Plus sign Match one or more of the previous
( ) Opening and closing parenthesis Group characters
[ ] Opening and closing square bracket Matches a range of characters
{ } Opening and closing curly brace Matches a specified number of occurrences of the previous

关于powershell - Powershell在搜索字符串后返回下一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57801742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com