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
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:
"..."
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.
Just another way using regex:
appcmd list apppool | % { [regex]::match( $_ , '(?<=")(.+)(?=")' ) } | select -expa value
or
appcmd list apppool | % { ([regex]::match( $_ , '(?<=")(.+)(?=")' )).value }
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]}