Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a command line like string?

Tags:

java

regex

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

  • "one quoted argument"
  • those
  • are
  • separate
  • "but not \"this one\""

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...).

like image 282
poussma Avatar asked May 08 '26 10:05

poussma


2 Answers

You may use the non greedy qualifier *? to make it work:

"(\\"|[^"])*?"|[^ ]+

See this link for an example in action: http://gskinner.com/RegExr/?32srs

like image 77
Alex Avatar answered May 10 '26 01:05

Alex


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.

like image 34
eis Avatar answered May 09 '26 23:05

eis



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!