none
Powershell regex working but -match regex doesnt work?! RRS feed

  • Question

  • Hello everyone,
    I have the following Code:

    $String ="/line/anotherline
    /line2/dasda 22 3g 7% /home
    line3 33 5g 10% /home2"

    $String.GetType() # = System.Object
    $String = $String -split "`r`n"
    $String.GetType() # = System.Array

    for($i = 0 ; $i -lt $String.Count ; $i++)
    {
         if($String[$i] -match "^[\/\w+|w+]\w+.*\%.*\w+$)
         {
              $Array += $String[$i] + "`n"
         }
         else
         {
              $a += $String[$i] + "`n"
         }
    }

    The regex working on regex101 but when i run it even like this:
    $String[0] -match "^[\/\w+|w+]\w+.*\%.*\w+$ # = False
    $String[1] -match "^[\/\w+|w+]\w+.*\%.*\w+$ # = False
    $String[2] -match "^[\/\w+|w+]\w+.*\%.*\w+$ # = False
    I get fail every single time!

    however when i do the following:
    $New_Array = @("/line/anotherline","/line2/dasda 22 3g 7% /home","line3 33 5g 10% /home2")
    and i test the following:
    $New_Array [0] -match "^[\/\w+|w+]\w+.*\%.*\w+$ # = False
    $New_Array [1] -match "^[\/\w+|w+]\w+.*\%.*\w+$ # = True
    $New_Array [2] -match "^[\/\w+|w+]\w+.*\%.*\w+$ # = True

    Why this is happening?
    I'm clueless...

    Thanks in advanced,
    Tal.
    Sunday, July 14, 2019 12:34 PM

Answers