You need to treat it as a single string like you are doing, but then your pattern has to account for all characters between the two values. That could include multiple new line/carriage returns since the amount of lines vary.
You need a nongreedy match. Hopefully the following examples answer your question.
First, a sample file
$tempfile = New-TemporaryFile
@'
a line with value1 in it
some line
and another line
a line with value2 in it
a line
a line with value1 in it
some line
and another line
yet another line
a line with value2 in it
a line with value1 in it
some line
and another line
yet another line
too many lines
a line with value2 in it
'@ | Set-Content $tempfile
Using Select-String
$text = Get-Content $tempfile -Raw
$pattern = '(?s)(value1).+?(value2)'
($text | Select-String -Pattern $pattern -AllMatches).Matches|ForEach-Object {
"----- This is one match -----"
$_.value
}
Using .Net [regex]
$text = Get-Content $tempfile -Raw
$pattern = '(value1).+?(value2)'
[regex]::Matches($text,$pattern,16)|ForEach-Object {
"----- This is one match -----"
$_.value
}
Output for both
----- This is one match -----
value1 in it
some line
and another line
a line with value2
----- This is one match -----
value1 in it
some line
and another line
yet another line
a line with value2
----- This is one match -----
value1 in it
some line
and another line
yet another line
too many lines
Interestingly if you use [regex]
and omit -Raw
from Get-Content
it will strip out the new line/carriage returns and give you the same text but in a single line.
----- This is one match -----
value1 in it some line and another line a line with value2
----- This is one match -----
value1 in it some line and another line yet another line a line with value2
----- This is one match -----
value1 in it some line and another line yet another line too many lines a line with value2