Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading