Regex 4 Command Line

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Marcel83
Forum Newbie
Posts: 5
Joined: Mon Oct 01, 2007 12:07 pm

Regex 4 Command Line

Post 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
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Regex 4 Command Line

Post 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?
Marcel83
Forum Newbie
Posts: 5
Joined: Mon Oct 01, 2007 12:07 pm

Post 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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Code: Select all

#^/(?P<commandtype>\S+)\s+(?P<command>-\S+)(?P<params>(?:\s+"[^"]+")+)#
Marcel83
Forum Newbie
Posts: 5
Joined: Mon Oct 01, 2007 12:07 pm

Post 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
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post 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";
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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);
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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);
Marcel83
Forum Newbie
Posts: 5
Joined: Mon Oct 01, 2007 12:07 pm

Post by Marcel83 »

Hello @ all,

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