Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 416
Gordon Avatar asked May 27 '26 20:05

Gordon


1 Answers

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.
like image 88
Wiktor Stribiżew Avatar answered May 30 '26 11:05

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!