How to parse a command line with regular expressions?

后端 未结 13 696
离开以前
离开以前 2020-12-03 16:02

I want to split a command line like string in single string parameters. How look the regular expression for it. The problem are that the parameters can be quoted. For exampl

13条回答
  •  粉色の甜心
    2020-12-03 16:09

    Without regard to implementation language, your regex might look something like this:

    ("[^"]*"|[^"]+)(\s+|$)
    

    The first part "[^"]*" looks for a quoted string that doesn't contain embedded quotes, and the second part [^"]+ looks for a sequence of non-quote characters. The \s+ matches a separating sequence of spaces, and $ matches the end of the string.

提交回复
热议问题