Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 282
mahesh Avatar asked Apr 24 '16 20:04

mahesh


1 Answers

[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 }
like image 92
Mathias R. Jessen Avatar answered Oct 10 '22 17:10

Mathias R. Jessen