mahesh
mahesh

Reputation: 478

extract all the strings between two characters powershell

Following are the two characters. 1. {{ 2. }} I'm trying to find all the strings between these characters in the following file.

    <add key="CSDFG_SqlServerFailOverInterval" value="{{environment}}"/>
    <add key="CSDFG_JobResetInterval" value="600000"/>
    <add key="FileTypeFilter" value="{{filetype}}"/>

and I'm using following powershell commands,

$x = C:\app.config
$s = Get-Content $x
$prog = [regex]::match($s,'{{([^/)]+)}}').Groups[1].Value
write-host $prog

and my output is just one string. it is not pulling all the strings. Can someone please suggest me how to get the all the strings. Thanks in advance.

Upvotes: 5

Views: 25813

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174920

[regex]::Match() only returns the first match. Use [regex]::Matches() to capture all matches:

$s = Get-Content 'C:\app.config'
[regex]::Matches($s, '{{([^/)]+)}}') |ForEach-Object { $_.Groups[1].Value }

Upvotes: 9

Related Questions