How to parse a command line with regular expressions?

后端 未结 13 678
离开以前
离开以前 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.

    0 讨论(0)
  • 2020-12-03 16:09

    (reading your question again, just prior to posting I note you say command line LIKE string, thus this information may not be useful to you, but as I have written it I will post anyway - please disregard if I have missunderstood your question.)

    If you clarify your question I will try to help but from the general comments you have made i would say dont do that :-), you are asking for a regexp to split a series of parmeters into an array. Instead of doing this yourself I would strongly suggest you consider using getopt, there are versions of this library for most programming languages. Getopt will do what you are asking and scales to manage much more sophisticated argument processing should you require that in the future.

    If you let me know what language you are using I will try and post a sample for you.

    Here are a sample of the home pages:

    http://www.codeplex.com/getopt (.NET)

    http://www.urbanophile.com/arenn/hacking/download.html (java)

    A sample (from the java page above)

     Getopt g = new Getopt("testprog", argv, "ab:c::d");
     //
     int c;
     String arg;
     while ((c = g.getopt()) != -1)
       {
         switch(c)
           {
              case 'a':
              case 'd':
                System.out.print("You picked " + (char)c + "\n");
                break;
                //
              case 'b':
              case 'c':
                arg = g.getOptarg();
                System.out.print("You picked " + (char)c + 
                                 " with an argument of " +
                                 ((arg != null) ? arg : "null") + "\n");
                break;
                //
              case '?':
                break; // getopt() already printed an error
                //
              default:
                System.out.print("getopt() returned " + c + "\n");
           }
       }
    
    0 讨论(0)
  • 2020-12-03 16:10
    \s*("[^"]+"|[^\s"]+)
    

    that's it

    0 讨论(0)
  • 2020-12-03 16:15

    Most languages have other functions (either built-in or provided by a standard library) which will parse command lines far more easily than building your own regex, plus you know they'll do it accurately out of the box. If you edit your post to identify the language that you're using, I'm sure someone here will be able to point you at the one used in that language.

    Regexes are very powerful tools and useful for a wide range of things, but there are also many problems for which they are not the best solution. This is one of them.

    0 讨论(0)
  • 2020-12-03 16:16

    I tend to use regexlib for this kind of problem. If you go to: http://regexlib.com/ and search for "command line" you'll find three results which look like they are trying to solve this or similar problems - should be a good start.

    This may work: http://regexlib.com/Search.aspx?k=command+line&c=-1&m=-1&ps=20

    0 讨论(0)
  • 2020-12-03 16:19

    This will split an exe from it's params; stripping parenthesis from the exe; assumes clean data:

    ^(?:"([^"]+(?="))|([^\s]+))["]{0,1} +(.+)$
    

    You will have two matches at a time, of three match groups:

    1. The exe if it was wrapped in parenthesis
    2. The exe if it was not wrapped in parenthesis
    3. The clump of parameters

    Examples:

    "C:\WINDOWS\system32\cmd.exe" /c echo this
    

    Match 1: C:\WINDOWS\system32\cmd.exe

    Match 2: $null

    Match 3: /c echo this

    C:\WINDOWS\system32\cmd.exe /c echo this
    

    Match 1: $null

    Match 2: C:\WINDOWS\system32\cmd.exe

    Match 3: /c echo this

    "C:\Program Files\foo\bar.exe" /run
    

    Match 1: C:\Program Files\foo\bar.exe

    Match 2: $null

    Match 3: /run

    Thoughts:

    I'm pretty sure that you would need to create a loop to capture a possibly infinite number of parameters.

    This regex could easily be looped onto it's third match until the match fails; there are no more params.

    0 讨论(0)
提交回复
热议问题