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
它返回两个记录(smith和smithpeter)->正确,但不是我想要的。
如果我尝试
[regex]$regex="smith,"
$myArray -match $regex
我没有结果。
我在网上找不到任何适当的答案。我相信在字符串中有逗号(“,”)的事实似乎正在引起查询问题。
感谢您的任何投入。
最佳答案
您只需要添加一个word boundary \b
圆smith
即可将其作为一个整体匹配。
[regex]$regex="\bsmith\b"
另请参阅What is a word boundary in regexes? SO帖子以获取更多详细信息。
关于regex - Powershell在具有特殊字符的数组中搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33042819/