Get the next string after validating patterns using powershell

I have a text file and the contents can be:

debug --configuration "Release" \p corebuild

Or:

-c "Dev" debug 

And now I have to validate the file to see if it has any pattern that matches --configuration or -c and print the string next to it

  • Pattern 1 – It should be Release
  • Pattern 2 – It should be Dev

How to achieve this in single command?

I tried below , but not sure how to extract only the release in the text , I only tried to see 1 pattern at a time

PS Z:\> $text = Get-Content 'your_file_path' -raw
PS Z:\> $Regex = [Regex]::new("(?<=\-\-configuration)(.*)")
PS Z:\> $Match = $Regex.Match($text)
PS Z:\> $Match.Value

 **Release /p net** 

Any help would be appreciated

>Solution :

If I understand correctly and you only care about extracting the argument to the parameters and not which parameter was used, this might do the trick:

$content = Get-Content 'your_file_path' -Raw

$re = [regex] '(?i)(?<=(?:--configuration|-c)\s")[a-z ]+'
$re.Matches($content).Value

See https://regex101.com/r/d2th35/1 for details.

From feedback in comments --configuration and -c can appear together, hence Regex.Matches is needed to find all occurrences.

Leave a Reply