简体   繁体   中英

Java/Scala regex, match all till word

I'm writing program that takes multiple variants of arguments as one string. I wanted to parse and validate this string by regex but unfortunately I didn't manage to write good enough regex.

So basically the regex should match this:

--image path_to_image Optionally(array of convertors like --flip x --brightness 60) (array of outputs)--output-console --output-file file_path

Now I'll write some valid string for my program:

--image image.jpg --output-console --output-file output.jpg
--image image.jpg --flip x --output-console --output-file output.jpg
--image image.jpg --flip x --brightness 60 --invertor --output-console

My best try is this:

            1.          2.      3.
--image ([^\s]+) (.*?--output)(-.*$)
  1. Group will take the image input name
  2. is supposed to take optional convertors that starts with -- and should end when --output is present
  3. takes the rest

My problem is that the 2nd group takes the --output into the group as well or

--image ([^\s]+) (.*?) (--output-.*$)

here if I write commands without convertors the --output will be in 2nd group.

How can I specify regex group that is simply optional and ends when --output is encountered?

https://regex101.com/

This is not an answer, but it will greatly help you to compose the correct regular expression. In the upper part, enter regex, in the lower part the text in which you want to search and it will show what your regular expression finds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM