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

它返回两个记录(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/

相关文章:

regex - 我们可以将值的正则表达式放入 DTD 中吗?

python - 意外的 re.sub 行为

javascript - 使用正则表达式从字符串获取数组

events - 在 Register-ObjectEvent 操作中修改 $eventargs

powershell - 基于文件内容的一线PowerShell批处理重命名

Python - 使用正则表达式解析 apache 配置

powershell - 无法在递归复制期间排除文件夹

azure - powershell 将 blob 上传到 azure blob 存储设置内容类型

powershell - Export-Csv - 访问路径 'C:\export.csv' 被拒绝

用于 TLS/SSL 密码套件强化的 Java 正则表达式