Gordon
Gordon

Reputation: 6863

RegEx within quotes

Given a string of something like

$uninstallString = '"C:\Autodesk\Dynamo\Core\Uninstall\unins000.exe" /SILENT' 

I am trying to use a RegEx to capture the contents of the quotes as well as the remainder after the the space. This is close...

$regularExpression = '\"(?<executable>([^\"])\")\s{1,}(?<arguments>(.+))'

but I get the closing quote in the executable capture. My understanding is that [^\"] is going to capture anything that isn't a quote, allowing the escaped quote that is outside the named capture to work. But of course my understanding is wrong in this case. ;) I will never need nested quotes, or single quotes, or apostrophe's, so I am trying to keep it as simple as possible and still capture what I need.

Upvotes: 3

Views: 63

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

Your executable group captures the trailing ", move the ) to the left:

$regularExpression = '"(?<executable>[^"]+)"\s+(?<arguments>.+)'

See the regex demo. Results:

enter image description here

Note I removed numbered capturing groups since they seem redundant for your scenario.

Details

  • " - a " char
  • (?<executable>[^"]+) - Group "executable": 1+ chars other than "
  • " - a " char
  • \s+ - 1+ whitespaces (note {1,} is equal to +)
  • (?<arguments>.+) - Group "arguments": any 1+ chars other than newline.

Upvotes: 3

Related Questions