Regex Powershell在具有特殊字符的数组中搜索字符串

Regex Powershell在具有特殊字符的数组中搜索字符串,regex,powershell,Regex,Powershell,Powershell问题: 我有一个数组$myArray,它包含以下值: $myArray[0]:smith,server1,1/1/2015 10:00:00 $myArray[1]:smithpeter,server1,1/1/2015 10:00:00 我想知道$myArray是否已经在任何地方包含值“smith”,但我不想在结果中包含“smith*”(例如smithpeter) 如果我尝试 [regex]$regex="smith" $myArray -match $regex [r

Powershell问题: 我有一个数组$myArray,它包含以下值:

$myArray[0]:smith,server1,1/1/2015 10:00:00
$myArray[1]:smithpeter,server1,1/1/2015 10:00:00
我想知道$myArray是否已经在任何地方包含值“smith”,但我不想在结果中包含“smith*”(例如smithpeter)

如果我尝试

[regex]$regex="smith"
$myArray -match $regex
[regex]$regex="smith,"
$myArray -match $regex
它返回两条记录(smith和smithpeter)-->正确,但不是我想要的

如果我尝试

[regex]$regex="smith"
$myArray -match $regex
[regex]$regex="smith,"
$myArray -match $regex
我没有得到任何结果

我在网上找不到合适的答案。我相信字符串中有一个逗号(“,”)这一事实似乎会导致查询出现一些问题

感谢您的输入。

您只需添加一个圆
smith
来匹配整个单词

[regex]$regex="\bsmith\b"

有关更多详细信息,请参见SO post。

除了单词边界,还可以使用插入符号“^”表示表达式的开始位置,使用美元符号“$”表示结束

[regex]$regex="^smith$"

试试
[regex]$regex=“\bsmith\b”
。很高兴它对你有用。请考虑通过点击来接受答案。✓ 在左边(参见)。