简体   繁体   English

Java/Scala 正则表达式,匹配所有单词

[英]Java/Scala regex, match all till word

I'm writing program that takes multiple variants of arguments as one string.我正在编写将 arguments 的多个变体作为一个字符串的程序。 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 --image path_to_image 可选(转换器数组,如 --flip x --brightness 60)(输出数组)--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应该采用以 -- 开头的可选转换器,并且应该在 --output 存在时结束
  3. takes the rest取rest

My problem is that the 2nd group takes the --output into the group as well or我的问题是第二组也将 --output 带入该组或

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

here if I write commands without convertors the --output will be in 2nd group.在这里,如果我编写没有转换器的命令,--output 将位于第二组。

How can I specify regex group that is simply optional and ends when --output is encountered?如何指定只是可选的正则表达式组,并在遇到 --output 时结束?

https://regex101.com/ 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.在上半部分输入正则表达式,在下半部分输入您要搜索的文本,它将显示您的正则表达式找到的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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