Select-String Regex

Welcome Forums General PowerShell Q&A Select-String Regex

This topic contains 1 reply, has 2 voices, and was last updated by

3 years, 10 months ago.

  • Author
  • #24254

    Points: 0
    Rank: Member

    I am reading a file using get-childitem and passing it to select-string like so:

    $regexTrans = 'Transaction Type: [a-z]+', 'Trade Amount: ', 'CUSIP Number: \d+[a-z]\d+', 'Settlement Date: \d{2}[/]\d{2}[/]\d{2}', 'Fund Name [a-z]+[a-z]+[a-z]+'
    $readfile = Get-ChildItem -Path C:\Test\FinancialStatements\FinStatement.txt
    $Line = ($readfile | Select-String $regexTrans -AllMatches).Matches.Value

    I am accessing the array like so:

    $line[2] and it work fine but if I need just part of array like the date instead of 'Settlement Date: \d{2}[/]\d{2}[/]\d' how can I just get the \d{2}[/]\d{2}[/]\d' part.

    I am still learning and never played with regex or arrays

    Thanks for helping me

    I am still learning

  • #24259

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    What you're talking about is called a capturing regex. You can define a portion of the regular expression as the bit you want to "keep," or "capture." These can be implemented using Named Groups in the expression. See for a basic example; the object returns by Select-String will have a Matches collection that includes whatever you captured.

The topic ‘Select-String Regex’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort