resistance has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

say we have this strange string:

c:\windows\folder.exe with .exe\our.exe prog.com -par1 /param2.exe
where:
c:\windows\ -folder folder.exe with .exe\ - folder with blanks in his name our.exe prog.com - the program with blanks in his name -par1 /param2.exe -parameters
of course it wasn't real example but its easy possible to construct.

I am trying to separate path to executable, executable and parameters. I can separate program with single .exe/.com/.bat extension by using:
=~/(.+?\.\w{3})( +)(.+)/
but this regexp fail with such extreme example.

It seems only regexp not enough here..

any ideas?

Replies are listed 'Best First'.
Re: Using regex to separate parameters
by ikegami (Patriarch) on Jul 06, 2008 at 14:08 UTC
    That command is ambiguous and would not work as intended. It should be
    "c:\windows\folder.exe with .exe\our.exe prog.com" -par1 /param2.exe

    By ambiguous, I mean there's no regexp pattern that will always work, so whatever solution we provide will be wrong. For example, if I give

    /^(.*) -/

    to extract the path, it'll fail for

    c:\files - work\foo
      That command is ambiguous

      yes but can be launched from registry or on many others ways,
      "" is a command processor completion (cmd.exe), it can work without "" too.
      for example I can run this from registry:
      taskmgr.exe 123
      if c:\windows\taskmgr.exe 123.exe exists ("taskmgr.exe 123.exe"). Windows will complete and quote the string.
      this can misslead because 123 is not a parameter but the part of file name.
        Quoting CreateProcess,

        If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string "c:\program files\sub dir\program name". This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:

        c:\program.exe files\sub dir\program name
        c:\program files\sub.exe dir\program name
        c:\program files\sub dir\program.exe name
        c:\program files\sub dir\program name.exe

        So if you want to behave like CreateProcess, you'll have to include file tests in your regexp or solution.

        But like I said above, this is a wrong answer, but it might be the wrong answer you're looking for.

        It's wrong cause it fails to handle "c:\program.exe files\sub" as "c:\program.exe files\sub.exe" if "c:\program.exe" exists.

        It's wrong cause it fails to handle "c:\program.exe files\sub" as "c:\program.exe" if "c:\program.exe" doesn't exists or is currently unavailable (say due to network problems).

        The fact that it's sometimes optional doesn't mean it's always optional.
        >c:\foo bar\test.pl 'c:\foo' is not recognized as an internal or external command, operable program or batch file. >"c:\foo bar\test.pl" Hello World

        The same applies for the registry where command lines are expected such as in file associations.

Re: Using regex to separate parameters
by Anonymous Monk on Jul 06, 2008 at 15:20 UTC
    Duplicate what microsoft does, either via Win32::API or Win32::TieRegistry