簡體   English   中英

Java/Scala 正則表達式,匹配所有單詞

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

我正在編寫將 arguments 的多個變體作為一個字符串的程序。 我想通過正則表達式解析和驗證這個字符串,但不幸的是我沒能寫出足夠好的正則表達式。

所以基本上正則表達式應該匹配這個:

--image path_to_image 可選(轉換器數組,如 --flip x --brightness 60)(輸出數組)--output-console --output-file file_path

現在我將為我的程序編寫一些有效的字符串:

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

我最好的嘗試是這樣的:

            1.          2.      3.
--image ([^\s]+) (.*?--output)(-.*$)
  1. 組將取圖像輸入名稱
  2. 應該采用以 -- 開頭的可選轉換器,並且應該在 --output 存在時結束
  3. 取rest

我的問題是第二組也將 --output 帶入該組或

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

在這里,如果我編寫沒有轉換器的命令,--output 將位於第二組。

如何指定只是可選的正則表達式組,並在遇到 --output 時結束?

https://regex101.com/

這不是一個答案,但它將極大地幫助您編寫正確的正則表達式。 在上半部分輸入正則表達式,在下半部分輸入您要搜索的文本,它將顯示您的正則表達式找到的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM