regex little frustation

Hello,

I was trying to get this right but I am not able to get the output in the way i want it.

I want to extract all the parameters which are parsed to this command as a regex

“C:\Program Files (x86)\AVEVA\Everything3D2.10\mon.exe” PROD E3D INIT T:\Scripts\Design\launch.init TTY MOA HZIENG/ENG /EXPORT $M/\sevrer.blablabla\dfs\EXPORT.mac

If i do something like this -match ‘(PROD.*$)’ then it works.

But what i was hoping was to match from the end of the string till the first quote ’ " ’ and this is where i didnt get my way around. I would appreciate your help.

Thanks a lot.
Cheers

What output are you looking for? This should give you an array of strings containing each parameter.

$string = @'
“C:\Program Files (x86)\AVEVA\Everything3D2.10\mon.exe” PROD E3D INIT T:\Scripts\Design\launch.init TTY MOA HZIENG/ENG /EXPORT $M/\\sevrer.blablabla\dfs\EXPORT.mac
'@

$string -match '(PROD.*$)' ; $Matches
($Matches[1] -split '\s')
## RESULTS
PROD
E3D
INIT
T:\Scripts\Design\launch.init
TTY
MOA
HZIENG/ENG
/EXPORT
$M/\\sevrer.blablabla\dfs\EXPORT.mac

You can use a positive lookbehind (?<=) to validate a starting double quote (^“) followed by non-double quotes ([^”]+) and a closing double quote. \s+ will match any spaces after the closing double quote.

$command = '"C:\Program Files (x86)\AVEVA\Everything3D2.10\mon.exe" PROD E3D INIT T:\Scripts\Design\launch.init TTY MOA HZIENG/ENG /EXPORT $M/\\sevrer.blablabla\dfs\EXPORT.mac'
$null = $command -match '(?<=^"[^"]+"\s+).*$'

$matches.0 # contains the arguments as if they are on the command line

$matches.0 -split ' ' # lists each argument as an element of an array

Hello,

Thanks for the feedback.

“random commandline” yes, this works. but actually my intention is not to match against the word “PROD” but to the ’ " ’

@AdminOfThings45. Thanks for your example. It works great.
But your logic is harder to understand for me.
My logic was starting from the end until it matches the first quote.
So using the .*$ and to stop once when it finds the first ‘"’ and this what I didnt manage to get it working in regex.
Do you know how this could be done?

Anyway, thanks a lot for your time and to get me on the right path.

Regards,
Jacobo.

 

 

 

Would it not be an option to just do this?

$string = '"C:\Program Files (x86)\AVEVA\Everything3D2.10\mon.exe" PROD E3D INIT T:\Scripts\Design\launch.init TTY MOA HZIENG/ENG /EXPORT $M/\\sevrer.blablabla\dfs\EXPORT.mac'

$arguments = $string -replace '".+"'

or

$string.Split('"')[-1]

The problem with pattern matching is that you need to understand your data. Doug’s examples work when there’s only one pair of double quotes. Since PowerShell and .NET don’t support match resets in regular expressions, it makes the regex string more verbose.

The reason

“.$
won’t work is because the first double quote is matched. Then .$ matches everything else until the end of the string. You will need to add something like a positive lookbehind to verify that only the second double quote matches. If there’s only the one pair of double quotes, a close alternative is
”[^"]*$
.

$string -replace '".+"' # Great until there are more than one pair of quotes
# Example
$string = '"C:\Program Files (x86)\AVEVA\Everything3D2.10\mon.exe" PROD E3D INIT "T:\Scripts\Design Stuff\launch.init" TTY MOA HZIENG/ENG /EXPORT $M/\\sevrer.blablabla\dfs\EXPORT.mac' 
$string -replace '".+"' # replaces the quoted arguments too
$string -replace '^".*?"' # works a lot better here because it is lazy matching with an anchor

Using -replace will be less cumbersome than -match if you are just trying to reduce the command string.

And yet another way …

$command.SubString($command.LastIndexOf(‘"’) + 1)