- This topic has 7 replies, 5 voices, and was last updated 3 days, 13 hours ago by
September 16, 2020 at 3:53 am #256952ParticipantTopics: 5Replies: 4Points: 32Rank: Member
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.
September 16, 2020 at 9:16 am #256997ParticipantTopics: 5Replies: 328Points: 522Rank: Major Contributor
What output are you looking for? This should give you an array of strings containing each parameter.PowerShell12345678910111213141516$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 -split '\s')## RESULTSPRODE3DINITT:\Scripts\Design\launch.initTTYMOAHZIENG/ENG/EXPORT$M/\\sevrer.blablabla\dfs\EXPORT.mac
September 16, 2020 at 9:35 am #257006ParticipantTopics: 0Replies: 78Points: 348Rank: Contributor
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.PowerShell123456$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
September 16, 2020 at 5:31 pm #257096ParticipantTopics: 5Replies: 4Points: 32Rank: Member
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.
September 16, 2020 at 6:18 pm #257105ParticipantTopics: 8Replies: 558Points: 2,136Rank: Community Hero
Would it not be an option to just do this?PowerShell1234567$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]
September 17, 2020 at 12:23 pm #257201ParticipantTopics: 0Replies: 78Points: 348Rank: Contributor
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.
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 isPowerShell1"[^"]*$
.PowerShell12345$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.
September 17, 2020 at 3:53 pm #257231ParticipantTopics: 7Replies: 121Points: 587Rank: Major Contributor
And yet another way …
$command.SubString($command.LastIndexOf('"') + 1)
- You must be logged in to reply to this topic.