Q:

PowerShell add variable output to regex group

$selected = 'File Name 123'
(Invoke-WebRequest -URI "$data").Content | 
Select-String -Pattern '(?:website data )(.*)(?:more data )([regex]::match($selected))(?: data)' -AllMatches | 
ForEach-Object { $_.Matches } | ForEach-Object { $_.Groups[2].Value }

If I replace ([regex]::match($selected)) with (.*) then I will get the correct result, but I need to insert the $selected variable text into that group, to find the correct value for Groups[1] is it possible, as my code doesn't work right now?
EDIT
Thanks to Olaf I fixed the script and it works now
$regex = '(?:website data )(.*)(?:more data )' + "($selected)" + '(?: data)'
(Invoke-WebRequest -URI "$data").Content |
Select-String -Pattern $regex -AllMatches | 
ForEach-Object { $_.Matches } | ForEach-Object { $_.Groups[2].Value }

A:

Used double quotes to expand variable instead of single quotes and used $regex to store regex groups. '($selected)' -> "($selected)"
$selected = 'File Name 123'
$regex = '(?:website data )(.*)(?:more data )' + "($selected)" + '(?: data)'
(Invoke-WebRequest -URI "$data").Content |
Select-String -Pattern $regex -AllMatches | 
ForEach-Object { $_.Matches } | ForEach-Object { $_.Groups[2].Value }

