Basically, I need to split the string like
"one quoted argument" those are separate arguments "but not \"this one\""
to get in result the list of arguments
This regex "(\"|[^"])*"|[^ ]+ nearly does the job but the issue is that regular expression always (at least in java) tries to match the longest string possible.
In consequence, when I apply the regex to a string that starts and ends with a quoted arguments, it matches the whole string and does not create a group for each argument.
Is there a way to tweak this regex or the matcher or the pattern or whatever to handle that?
Note: don't tell me I could use GetOpt or CommandLine.parse or anything else similar.
My concern is about pure java regex (if possible but I doubt it...).
You may use the non greedy qualifier *? to make it work:
"(\\"|[^"])*?"|[^ ]+
See this link for an example in action: http://gskinner.com/RegExr/?32srs
regular expression always (at least in java) tries to match the longest string possible.
Um... no.
That is controlled by if you use greedy or non-greedy expressions. See some examples. Using a non-greedy one (by adding a question mark) should do it. It's called lazy quantification.
The default is greedy, but it certainly doesn't mean it is always that way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With