Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Arrays 查找powershell中特定字符串中包含的数组项_Arrays_Powershell - Fatal编程技术网

Arrays 查找powershell中特定字符串中包含的数组项

Arrays 查找powershell中特定字符串中包含的数组项,arrays,powershell,Arrays,Powershell,我有一个字符串,我知道它将包含字符串数组中的一个条目。我正在查找字符串包含的数组项 如果我有以下变量: $String = "This is my string" $Array = "oh","my","goodness" 我可以通过以下方式验证字符串中是否存在任何数组项: $String -match $($Array -join "|") 但是,这只会返回true或false。我想知道哪个

我有一个字符串,我知道它将包含字符串数组中的一个条目。我正在查找字符串包含的数组项

如果我有以下变量:

$String = "This is my string"
$Array = "oh","my","goodness"
我可以通过以下方式验证字符串中是否存在任何数组项:

$String -match $($Array -join "|")
但是,这只会返回true或false。我想知道哪个条目是匹配的

-Contains看起来方向正确,但这只验证数组是否包含特定对象,我想验证字符串中包含哪些数组项。我认为类似于foreach循环的方法可以实现这一点,但这似乎是一种资源密集型的解决方法


感谢您的帮助

这就是自动变量
$Matches
的作用

返回:

PS /> 

Name                           Value
----                           -----
0                              my
PS />

Groups Success Name Captures Index Length Value
------ ------- ---- -------- ----- ------ -----
{0}       True 0    {0}          0      4 This
{0}       True 0    {0}          8      2 my
这是查看匹配项的另一种方式:

# Adding "This" for the example

$String = "This is my string"
[regex]$Array = "oh","my","goodness","This" -join '|'

$Array.Matches($String)
返回:

PS /> 

Name                           Value
----                           -----
0                              my
PS />

Groups Success Name Captures Index Length Value
------ ------- ---- -------- ----- ------ -----
{0}       True 0    {0}          0      4 This
{0}       True 0    {0}          8      2 my

这太完美了,谢谢!