There is room for improvement here since we don't have a sample file but your regex pattern appears to function for something like this.
**USE**AdventureWorks2012**GO**
I suspect that is not how the lines look in your data file. Possibly that is split across lines? I would like to introduce you to Select-String
to help mitigate this. Consider the following file of bad sql (for testing) I created from TechNet.
USE AdventureWorks2012
GO
DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, World.'
GO -- @MyMsg is not valid after this GO ends the batch. Just because.
**USE** AdventureWorks2012;
**GO**
DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, World.'
Now, in that file we have an example that has asterisks and one that does not. Your regex is expecting **
so I have included solutions to cover both scenarios.
# Test with one pattern and comment out the other
$pattern = "(?<=USE\*\*)([\s\S]*?)(?=\*\*GO)"
$pattern = "(?<=USE)([\s\S]*?)(?=GO)"
(Get-Content $path -Raw | Select-String -Pattern $pattern -AllMatches -CaseSensitive).Matches.Value | ForEach-Object{$_.Trim()}
Use the $pattern
that actually matches your text. If it does spread across lines this will work as we read the file in as one string (Would be slow with large files). -CaseSensitive
should help in case there is a comment in the text that had the word "because" for instance. Another important change is that the match was made non-greedy in case there are multiple matches in the file.
Patterns should return AdventureWorks2012;
and AdventureWorks2012
respectively.
Edit with your sample
Using the following code based on my explanation above you get your expected results.
$pattern = "(?<=USE)([\s\S]*?)(?=GO)"
$arr = (Get-Content $path -Raw | Select-String -Pattern $pattern -AllMatches -CaseSensitive).Matches.Value | ForEach-Object{$_.Trim()}