Page 1 of 1

Regex 4 Command Line

Posted: Mon Oct 01, 2007 12:16 pm
by Marcel83
Hello,

At first, sorry for my bad english. i will try my best.

I will parse an Commandline like this:

/commandtype -command "param1" "param2" "param3 mybe with whitespaces"

or:

/cmd -sms "My Message Header" "My Message to you"

My Regex:

^\/([a-z]+)\s(-[a-z]+)(\s"[^"]+")*

commandtype and command will be parsed correctly. But the params, we will say in german like -> "they going under". Anybody have an Idea? I try it since 5 Days, but i suck :(

Can anybody help me?


Greetz
Marcel

Re: Regex 4 Command Line

Posted: Mon Oct 01, 2007 12:28 pm
by Zoxive
Marcel83 wrote: ^\/([a-z]+)\s(-[a-z]+)(\s"[^"]+")*
I'm not so good at regex, but why do you have * at the end? If you change it to a + It wont match just anything.

Code: Select all

^\/([a-z]+)\s(-[a-z]+)(\s"[^"]+")+
But that is only Matching all of the command, what is the use of this?

Posted: Mon Oct 01, 2007 12:41 pm
by Marcel83
I will implement it in an Chatsystem written by me with Javascript and PHP. But its not only a simple chat, its a CommandLine too. Later, I will be able to write commands like /cmd -copy "CommunityPictureFolder" "PictureFolderDestiny" "-r" or anything. Well, i will handle any Procedures with the Inputfield from the Chat. Later, in few Months, mybe i will write an OnlineOS. There will i need an CommandLineInterface.

What i wanna know?

Which CommandType is in the Inputfield? (/cmd or /set or /call or anything....)
Which Command is in the........ (-sms, -copy, -kick, -ban, or anything)
Which Params are in the...... ("Username", "PicturePath", "Options lik r for rekursive or so...")


I hope u will understand what i want......


Greetz Marcel

Posted: Mon Oct 01, 2007 3:08 pm
by GeertDD

Code: Select all

#^/(?P<commandtype>\S+)\s+(?P<command>-\S+)(?P<params>(?:\s+"[^"]+")+)#

Posted: Mon Oct 01, 2007 5:42 pm
by Marcel83
Hello GeertDD,

Thank you for your answer. Thats a very nice Regex, where i learned how can is use aliases in regex. (One way closer to the Finish?!?!?)

But, well, i get this:

Array
(
[0] => /commandtype -command "param1" "param2" "param3 mybe with whitespaces"
[commandtype] => commandtype
[1] => commandtype
[command] => -command
[2] => -command
[params] => "param1" "param2" "param3 mybe with whitespaces"
[3] => "param1" "param2" "param3 mybe with whitespaces"
)

No i have the same Problem like my regex. My (and your) regex is close to my destination. But the params all in one string. But i want the params in an extra array point, even param, in even arraypoint. Well, im so sorry. I think i don't have explained my Problem very well at my First Question... (and mybe at the second zo) :(

My Destination is an array like this:

Array
(
[0] => /commandtype -command "param1" "param2" "param3 mybe with whitespaces"
[commandtype] => commandtype
[1] => commandtype
[command] => -command
[2] => -command
[params] => array(
[0] => "param1"
[1] => "param2"
[2] => "param3 mybe with whitespaces"
)
[3] => array(
[0] => "param1"
[1] => "param2"
[2] => "param3 mybe with whitespaces"
)
)

Well, i don't know it, but, is it possible? and when it is, how?



Thank you all till now for your answers and your patience.

Greetz Marcel

Posted: Mon Oct 01, 2007 6:07 pm
by mrkite
As far as I know, you can't get subarrays returned from preg_match

Why cram it all into one regex anyway?

Code: Select all

$line='/cmd -sms "My Message Header" "My Message to you" open text \'and "mixed" quotes\'';

if (preg_match('{/cmd\s+\-(\w+)\s*(.*)$}',$line,$matches))
{
        $command=$matches[1];
        preg_match_all('{("[^"]*"|\'[^\']*\'|[^\s]+)}',$matches[2],$params);

        echo "Command: $command\n";
        foreach ($params[1] as $i=>$param)
                echo "Param $i: $param\n";
}

Posted: Mon Oct 01, 2007 6:33 pm
by VladSun
My solution:

Code: Select all

$input = '/commandtype -command "param1" "param2" "param3 mybe with whitespaces"';
$regexp = '/\/(\w+)\s-(\w+)\s"(.+?)"\s"(.+?)"\s"(.+?)"/';
$matches = array();
preg_match($regexp, $input, $matches);

print_r($matches);

Posted: Tue Oct 02, 2007 1:24 pm
by GeertDD
I'd say just catch all parameters as in my regex and then explode them with a second regex. As mrkite said you can't get them returned as a subarray directly.

Code: Select all

preg_match_all('/"[^"]*"/', '"param1" "param2" "param3 mybe with whitespaces" ', $matches);

Posted: Tue Oct 02, 2007 5:38 pm
by Marcel83
Hello @ all,

Well, ok. I think thats the way it is. Thank you all for your answers.