Powershell - print only text between quotes?

后端 未结 3 1883
孤城傲影
孤城傲影 2021-01-21 17:45

How can I have the output of the following text only show the text in the quotes (without the quotes)?

Sample text\"

this is an \"apple\". it is red
this         


        
相关标签:
3条回答
  • 2021-01-21 18:00

    A concise solution based on .NET method [regex]::Matches(), using PSv3+ syntax:

    $str = @'
    this is an "apple". it is red
    this is an "orange". it is orange
    this is an "blood orange". it is reddish
    '@
    
    [regex]::Matches($str, '".*?"').Value -replace '"'
    

    Regex ".*?" matches "..."-enclosed tokens and .Matches() returns all of them; .Value extracts them, and -replace '"' strips the " chars.

    This means that the above even works with multiple "..." tokens per line (though note that extracting tokens with embedded escaped " chars. (e.g, \") won't work).


    Use of the -match operator - which only looks for a (one) match - is an option only if:

    • you split the input into lines
    • and each line contains at most 1 "..." token (which is true for the sample input in the question).

    Here'a PSv4+ solution:

    # Split string into lines, then use -match to find the first "..." token
    ($str -split "`r?`n").ForEach({ if ($_ -match '"(.*?)"') { $Matches[1] } })  
    

    Automatic variable $Matches contains the results of the previous -match operation (if the LHS was a scalar) and index [1] contains what the 1st (and only) capture group ((...)) matched.


    It would be handy if -match had a variant named, say, -matchall, so that one could write:

    # WISHFUL THINKING (as of PowerShell Core 6.2)
    $str -matchall '".*?"' -replace '"'
    

    See this feature suggestion on GitHub.

    0 讨论(0)
  • 2021-01-21 18:03

    Just another way using regex:

    appcmd list apppool | % { [regex]::match( $_ , '(?<=")(.+)(?=")' ) } | select -expa value
    

    or

     appcmd list apppool | % { ([regex]::match( $_ , '(?<=")(.+)(?=")' )).value }
    
    0 讨论(0)
  • 2021-01-21 18:16

    here is one way

    $text='this is an "apple". it is red
    this is an "orange". it is orange
    this is an "blood orange". it is reddish'
    
    $text.split("`n")|%{
    $_.split('"')[1]
    }
    

    This is the winning solution

    $text='this is an "apple". it is red
    this is an "orange". it is orange
    this is an "blood orange". it is reddish'
    
    $text|%{$_.split('"')[1]}
    
    0 讨论(0)
提交回复
热议问题