Select-String Regex

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 1 year, 6 months ago.

  • Author
  • #24254
    Profile photo of Wilfredo Perez
    Wilfredo Perez

    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
    Profile photo of Don Jones
    Don Jones

    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.

You must be logged in to reply to this topic.