Reputation: 445
I have the following RegEx that mostly works for my use cases (CLI command interpreter):
[^\s"]+|"[^"\\]*(?:\\.[^"\\]*)*"
It matches the following scenarios (and separates parts successfully):
foo list
foo list --barId ae920cf4-79e7-4c6f-8420-2d64cd6e4ae2
foo list --name "foo's name"
foo create --description "this is a \"description\" with some escaped quotes"
The missing piece is where I have escaped double quotes that are not wrapped by outer, un-escaped double quotes. Example:
bar create --expression service.GetData(\"2194c75c-26be-405a-b264-5a96152d93f8\")
Here, the entire "service.GetData" statement should be a single match, but it breaks at the (escaped) double quote. If I wrap the parameter value in double quotes, it works (as it does in the previous example block). Unfortunately though, I need it to also work when the parameter value is not wrapped in double quotes.
Can someone help with that last piece?
Upvotes: 0
Views: 199
Reputation: 445
I ended up just wrapping all param values with double-quotes from the CLI client application. This made the original RegEx work just fine.
Upvotes: 0
Reputation: 3663
Can you use single quotes? As if so then you can put your quote capturing section first so it greedily matches.
(?:"|')[^"\\]*(?:\\.[^"\\]*)*(?:"|')|[^\s"]+
That will then match the following:
bar create --expression 'service.GetData(\"2194c75c-26be-405a-b264-5a96152d93f8\")'
Otherwise you have the problem of how do you differentiate between sections. Unless you can make a further constraint upon spacing. If you are able to say that you can't pass spaces in an argument that isn't wrapped in quotes then you can do:
"[^"\\]*(?:\\.[^"\\]*)*"|([^\s])+|[^\s"]+
Upvotes: 1