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
这太完美了,谢谢!