Regex 带正则表达式的条件If

Regex 带正则表达式的条件If,regex,powershell,if-statement,multiple-matches,Regex,Powershell,If Statement,Multiple Matches,我正在做一个函数来尝试一些正则表达式。让我解释一下 function traitement { if ($Matches.NAME -match "^A_(?<test1>[\w{1,6}]{1,7})") { [void]($memberOfCollection.add($Matches.test1)) } elseif ($Matches.NAME -match "^A_(?<test2>[]*)") {

我正在做一个函数来尝试一些正则表达式。让我解释一下

function traitement
{
    if ($Matches.NAME -match "^A_(?<test1>[\w{1,6}]{1,7})")
    {
        [void]($memberOfCollection.add($Matches.test1))
    }
    elseif ($Matches.NAME -match "^A_(?<test2>[]*)")
    {
         [void]($memberOfCollection.add($Matches.test2))
    }
    else
    {
        [void]($memberOfCollection.add($Matches.NAME))
    }
}
功能训练
{
if($Matches.NAME-match“^A(?[\w{1,6}]{1,7})”)
{
[void]($memberOfCollection.add($Matches.test1))
}
elseif($Matches.NAME-match“^A_(?[]*)”)
{
[void]($memberOfCollection.add($Matches.test2))
}
其他的
{
[void]($memberOfCollection.add($Matches.NAME))
}
}
我有
$Matches.NAME
返回字符串,如
“A\u UserINTEL”
,“A
\u UserINTELASUS”
“A\u UserINTEL\u Adobe”

我需要区分来自
$Matches.NAME
的两个字符串,并因此编写几个测试

  • “A_UserINTEL”
    “A_UserINTELASUS”
    必须返回
    “UserINTEL”

  • “用户英特尔Adobe
    ”必须返回
    “用户英特尔Adobe”

Test1允许我检索
“UserINTEL”
,但我没有成功地通过test2将
“UserINTEL\u Adobe”


有什么想法吗?多谢各位

有一个;方法不止一种,特别是对于正则表达式,但这里有一种方法:

function traitement {
    # just for more clarity in the rest of the code
    $name = $Matches.NAME
    if ($name -match '^A_UserIntel(?:ASUS)?$') {
        # the regex tests for "A_UserINTEL" or "A_UserINTELASUS"
        [void]($memberOfCollection.add("UserINTEL"))
    }
    elseif ($name -match '^A_UserIntel_Adobe$') {
        # this elseif is basically the same as 
        # elseif ($name -eq 'A_UserIntel_Adobe') {
        # no real need for regex there..
        [void]($memberOfCollection.add("UserINTEL_Adobe"))
    }
    else {
        [void]($memberOfCollection.add($name))
    }
}

尝试使用
*
而不是
[]*