powershell字符串比较运算符忽略通配符

时间:2014-04-02 14:16:20

标签: regex powershell substring wildcard

我正在尝试比较这样的字符串:

'Backup Exec Alert: Job Success (Server: "AMSMGMT") (Job: "Ams-Daily-Back")' -match '(Server: "AMSMGMT") (Job: "Ams-Daily-Back")'

这是假的,因为我需要看看通配符,例如

'/(Server: "AMSMGMT"/) /(Job: "Ams-Daily-Back"/)'

问题:

是否有任何powershell函数可以找到忽略通配符/正则表达式的子字符串?

非常感谢

2 个答案:

答案 0 :(得分:2)

由于您正在进行文字字符串匹配,所以-like可能是比-match更好的选择

'Backup Exec Alert: Job Success (Server: "AMSMGMT") (Job: "Ams-Daily-Back")' -like '*(Server: "AMSMGMT") (Job: "Ams-Daily-Back")'

True

答案 1 :(得分:0)

如前所述,更合适,但您也可以使用“Escape”

'Backup Exec Alert: Job Success (Server: "AMSMGMT") (Job: "Ams-Daily-Back")' -match [System.Text.RegularExpressions.Regex]::Escape('(Server: "AMSMGMT") (Job: "Ams-Daily-Back")')

或C#包含方法

'Backup Exec Alert: Job Success (Server: "AMSMGMT") (Job: "Ams-Daily-Back")'.Contains('(Server: "AMSMGMT") (Job: "Ams-Daily-Back")')