Regex 使用Where对象与数组列表匹配
我已经找到了很多我在这里尝试的例子,但由于某些原因,它不起作用 我有一个正则表达式列表,我正在对照单个值进行检查,但似乎找不到匹配项 我正在尝试匹配域。e、 gmail.com、yahoo.com、live.com等 我正在导入csv以获取域,并已调试此代码以确保值符合我的预期。e、 gmail.com 正则表达式示例AKA$finalwhitelistaray 代码Regex 使用Where对象与数组列表匹配,regex,powershell,Regex,Powershell,我已经找到了很多我在这里尝试的例子,但由于某些原因,它不起作用 我有一个正则表达式列表,我正在对照单个值进行检查,但似乎找不到匹配项 我正在尝试匹配域。e、 gmail.com、yahoo.com、live.com等 我正在导入csv以获取域,并已调试此代码以确保值符合我的预期。e、 gmail.com 正则表达式示例AKA$finalwhitelistaray 代码 提前感谢您提供的帮助。当前代码的问题是您将正则表达式放在了-match操作符的左侧。[咧嘴笑]把它和你的代码换成工作 考虑到Lo
提前感谢您提供的帮助。当前代码的问题是您将正则表达式放在了-match操作符的左侧。[咧嘴笑]把它和你的代码换成工作 考虑到LotPings指出的区分大小写,并使用正则表达式或符号对每个URL进行一次测试,下面是其中一些测试的演示。\b表示单词边界,|表示正则表达式或符号。$RegexURL_白名单部分从第一个数组构建regex模式。如果我没有说清楚,请问
$URL_WhiteList = @(
'gmail.com'
'yahoo.com'
'live.com'
)
$RegexURL_WhiteList = -join @('\b' ,(@($URL_WhiteList |
ForEach-Object {
[regex]::Escape($_)
}) -join '|\b'))
$NeedFiltering = @(
'example.com/this/that'
'GMail.com'
'gmailstuff.org/NothingElse'
'NotReallyYahoo.com'
'www.yahoo.com'
'SomewhereFarAway.net/maybe/not/yet'
'live.net'
'Live.com/other/another'
)
foreach ($NF_Item in $NeedFiltering)
{
if ($NF_Item -match $RegexURL_WhiteList)
{
'[ {0} ] matched one of the test URLs.' -f $NF_Item
}
}
输出
[ GMail.com ] matched one of the test URLs.
[ www.yahoo.com ] matched one of the test URLs.
[ Live.com/other/another ] matched one of the test URLs.
$ListOfDomains和$finalwhitelistaray中的每一个都是什么?编辑了这篇文章,使其更具描述性。由于-match操作符是基于RegEx的:$ListOfDomains=@gmail\.com、yahoo\.com、live\.com或使用替代$ListOfDomains=gmail\.com | yahoo\.com | live\.com,顺便说一句,PowerShell中的操作符默认不区分大小写,那么这个?我没有必要`@先生-谢谢!现在我可以看出你的正则表达式在-match操作符的错误一侧。[grin]试着换掉这两个,你就可以得到你的对手了。//我同意LotPings的说法,你不需要?i这个东西,而且你可能会使用正则表达式或那些正则表达式模式。@mr-kool!谢谢你的提醒。。。我很高兴你能按需要工作![咧嘴笑]
$URL_WhiteList = @(
'gmail.com'
'yahoo.com'
'live.com'
)
$RegexURL_WhiteList = -join @('\b' ,(@($URL_WhiteList |
ForEach-Object {
[regex]::Escape($_)
}) -join '|\b'))
$NeedFiltering = @(
'example.com/this/that'
'GMail.com'
'gmailstuff.org/NothingElse'
'NotReallyYahoo.com'
'www.yahoo.com'
'SomewhereFarAway.net/maybe/not/yet'
'live.net'
'Live.com/other/another'
)
foreach ($NF_Item in $NeedFiltering)
{
if ($NF_Item -match $RegexURL_WhiteList)
{
'[ {0} ] matched one of the test URLs.' -f $NF_Item
}
}
[ GMail.com ] matched one of the test URLs.
[ www.yahoo.com ] matched one of the test URLs.
[ Live.com/other/another ] matched one of the test URLs.