lundi 30 juillet 2018

Conditional If with REGEX

I'm working on a function to try some regex. Let me explain.

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))
    }
}

I have $Matches.NAME return string like "A_UserINTEL", "A_UserINTELASUS" or "A_UserINTEL_Adobe"

I need to differentiate 2 strings coming from $Matches.NAME and therefore write several tests.

  • "A_UserINTEL" and "A_UserINTELASUS" must return "UserINTEL".

  • "A_UserINTEL_Adobe" must return "UserINTEL_Adobe"

Test1 allows me to retrieve "UserINTEL" but I didn't succeed test2 to bring me "UserINTEL_Adobe".

Any idea? Thank you.

Aucun commentaire:

Enregistrer un commentaire