locked
Powershell Regular Expressions Help RRS feed

  • Question

  • Hello,

    I've normally done regular expressions in Perl, as I am learning PowerShell I have an expression below that works within Perl but not within PowerShell. Do I need to make any changes to the regular expression for PowerShell to process this or am I missing something totally different?

    Expression:

    (^\d+).​*STRING 1 (.*​) \*+.​*\n(?:^STRING 2 (.*​)\.\s+\n)?^STRING 3\(s\) STRING 4 (.*)\n

    Code with Expression

    $myReg = "(^\d+).​*STRING 1 (.*​) \*+.​*\n(?:^STRING 2 (.*​)\.\s+\n)?^STRING 3\(s\) STRING 4 (.*)\n"
    $myPath = "C:\MyFile\file.txt"
    Select-String -Path $myPath -Pattern $myReg

    Any help would be greatly appreciated, Thank you!


    Regards, Yarrokon

    Tuesday, June 7, 2016 5:31 PM

Answers

  • Hello,

    Figured the issue out. For some reason Select-String does not like to parse multiple lines with the regex I'm using. Making the below changes has resolved this and pulls the results with the expression as it would in Perl.

    $myPath = "MyFile.txt"
    $myReg = "(^\d+).​*STRING 1 (.*​) \*+.​*\n(?:^STRING 2 (.*​)\.\s+\n)?^STRING 3\(s\) STRING 4 (.*)\n"
    $content = Get-Content -Raw $myPath
    [regex]::Matches($content, $myReg, [System.Text.RegularExpressions.RegexOptions]::Multiline)


    Regards, Yarrokon

    • Marked as answer by Bill_Stewart Tuesday, July 12, 2016 9:33 PM
    Tuesday, June 7, 2016 9:51 PM

All replies

  • What do you mean by "works within Perl but not within PowerShell"?

    I don't think the regulars in this forum are intimately familiar with Perl regexes, so you're going to need to be more specific.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 7, 2016 5:39 PM
  • Perl RegEx flaovor is not the same as Microsoft and PowerShell.  It has many subtle variations.

    You should give an example of what you are trying to do.


    \_(ツ)_/

    Tuesday, June 7, 2016 6:26 PM
  • Hello,

    Figured the issue out. For some reason Select-String does not like to parse multiple lines with the regex I'm using. Making the below changes has resolved this and pulls the results with the expression as it would in Perl.

    $myPath = "MyFile.txt"
    $myReg = "(^\d+).​*STRING 1 (.*​) \*+.​*\n(?:^STRING 2 (.*​)\.\s+\n)?^STRING 3\(s\) STRING 4 (.*)\n"
    $content = Get-Content -Raw $myPath
    [regex]::Matches($content, $myReg, [System.Text.RegularExpressions.RegexOptions]::Multiline)


    Regards, Yarrokon

    • Marked as answer by Bill_Stewart Tuesday, July 12, 2016 9:33 PM
    Tuesday, June 7, 2016 9:51 PM
  • Perl RegEx flaovor is not the same as Microsoft and PowerShell.  It has many subtle variations.

    You should give an example of what you are trying to do.


    \_(ツ)_/


    I've not known Regexp to be written differently from one language to the next unless you are using something more native commands that have their own options for searching, like findstr vs Get-ChildItem.

    From this page: http://www.regular-expressions.info/powershell.html

    Regards, Yarrokon

    Tuesday, June 7, 2016 9:56 PM
  • Perl RegEx flaovor is not the same as Microsoft and PowerShell.  It has many subtle variations.

    You should give an example of what you are trying to do.


    \_(ツ)_/


    I've not known Regexp to be written differently from one language to the next unless you are using something more native commands that have their own options for searching, like findstr vs Get-ChildItem.

    From this page: http://www.regular-expressions.info/powershell.html

    Regards, Yarrokon

    Over time RegEx has changed.  Vendors have added extras.  What we used backi  1983 is way less embellished then today.  Most dialects are in  sync today but many issues remain.

    Yes MS Select-String parses one line at a time unless you set the options or read the file "Raw"

    Get-Content -Path$myPath  -Raw |Select-String -Pattern$myReg

    This is just something you need to understand about Windows and RegEx dialects.

    There is no standard for extensions or implementations.


    \_(ツ)_/

    Tuesday, June 7, 2016 10:28 PM
  • Options are implemented differently by each vendor.

    (?opt opt opt)


    \_(ツ)_/

    Tuesday, June 7, 2016 10:31 PM