Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 从PowerShell中的正则表达式搜索获取捕获数组_Regex_Powershell - Fatal编程技术网

Regex 从PowerShell中的正则表达式搜索获取捕获数组

Regex 从PowerShell中的正则表达式搜索获取捕获数组,regex,powershell,Regex,Powershell,假设我有这个字符串: "14 be h90 bfh4" 我有一个正则表达式模式: "(\w+)\d" 在PowerShell中,如何获取包含{h,bfh}内容的数组?如果要捕获一个或多个后跟数字的字母表,则要捕获的正则表达式如下: [a-zA-Z]+(?=\d) 同样的powershell代码是这样的 $str = "14 be h90 bfh4" $reg = "[a-zA-Z]+(?=\d)" $spuntext = $str | Select-String $reg -AllMatc

假设我有这个字符串:

"14 be h90 bfh4"
我有一个正则表达式模式:

"(\w+)\d"

在PowerShell中,如何获取包含{h,bfh}内容的数组?

如果要捕获一个或多个后跟数字的字母表,则要捕获的正则表达式如下:

[a-zA-Z]+(?=\d)
同样的powershell代码是这样的

$str = "14 be h90 bfh4"
$reg = "[a-zA-Z]+(?=\d)"
$spuntext = $str | Select-String $reg -AllMatches |
            ForEach-Object { $_.Matches.Value }
echo $spuntext
免责声明:我几乎不懂powershell脚本语言,因此您可能需要调整一些代码。

稍微缩短版本:

@(Select-String "[a-zA-Z]+(?=\d)" -Input "14 be h90 bfh4" -AllMatches).Matches.Value

其他答案展示了多种剥猫皮的方法。另一种方法是使用.Net提供的[regex]对象

$regex = [regex] '([a-z]+)(?=\d+)'
$regex.Matches("14 be h90 bfh4") | Select Value

我不太擅长正则表达式,但我正在努力学习。这是你想要的吗$array=$string-split'\s'-替换'[^a-zA-Z-]',|SELECT-string-Pattern'[a-Z]'Try$result=SELECT-string'\b\p{L}+\d+\b'-Input 14 be h90 bfh4-AllMatches |%{$\ Matches.Groups[1].Value}powershell部分确实需要返工,但其要点看起来不错,谢谢@lievenkersmaekers。实际上,我只是在谷歌上搜索了一下powershell,然后想出了这个。我以前从来没有机会使用powershell。但好的是,每件事都有第一次: