Tutorial :Regular expression prints one out of two in matching pattern



Question:

Hey guys, question about regex in PHP!

For the given pattern, kinda like a shell terminal syntax:

application>function -arg1 value -arg2 value -arg3 value -arg4 value  

I want to parse the arguments. This is my regex code

$command=' -arg1 value -arg2 value -arg3 value -arg4 value ';    // note the string begins by a space character and ends by a space character  // now i'm trying to parse arguments    $cmd->arguments=new \stdClass();    preg_replace('`\s-(.*)\s([a-zA-Z0-9-_]*)\s`Ue',  '$cmd->arguments->$1="$2";',$command);    // this regex will pick one matching out of two and returns    $cmd->arguments=stdClass(        [arg1]=>value,      [arg3]=>value    )  

arg2 and arg4 are skipped. Any idea why? Thanks in advance!


Solution:1

To answer your question: You have a space \s both at the start and at the end of your regex, so after the first match arg1, the first occurrence of \s- is at arg3 because the space you are searching for before arg2 has already been matched at the end of the first match.

It also might be easier to just trim() the string and then split() / explode() it at the spaces.

Edit: By the way, removing the \s at the end should solve your problem.


Solution:2

Use getopt() http://www.tuxradar.com/practicalphp/21/2/4


Solution:3

As jeroen said, your specific issue is the \s at the beginning and end of your regex.

It is easy to rewrite this regex so that the spaces are not needed at all, except in between the arg and value. Consider this regex:

-(.*?)\s+([a-zA-Z0-9-_]*)  

Matches:

    -arg1 value -arg2 value -arg3 value  -arg4  value  //spaces before and between...  -arg1 value    -arg2 value -arg3 value   -arg4    value //no lead spaces...        

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »