in reply to Using regex to separate parameters

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

Replies are listed 'Best First'.
Re^2: Using regex to separate parameters
by resistance (Beadle) on Jul 06, 2008 at 14:26 UTC
    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).

        Thanks ikegami for your explanation, actually my script run on linux and it parse data which it get from windows registry. This input data dont follow logic and can be simple malicious. I cannot use any of Win32 modules which using XS part. The command is ambiguous because it intend to be ambiguous to misslead users and standard programs like antiviruses etc.

        right now I stick to:
        sub find_parameters { my $input = $_[0]; if($input =~ /^(.+?\.\w{3})$/){ print "only one executable: $input\n"; }elsif($input =~ /^(.+?\.\w{3})( +)([\/\-].+)$/ && -f $1 ){ print "$1 with parameters $3\n"; }elsif($input =~ /^“(.+)”$/){ # strip quotes and parse again find_parameters($1); }else{ print "not parsed: $input\n"; }; };
      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.

        'c:\foo' is not recognized as an internal or external command,

        this error message occur because you type this manually in cmd.exe

        in cmd.exe this will work:
        foo.exe bar
        if "foo.exe bar.exe" exists in the %path%

        this was not my question, thanks anyway