Divide arguments with regex

0

I have difficulties with regex. I just need to split a string of entries.

Input: "arg1 arg2 arg3 arg4"; Output: [arg1, arg2, arg3, arg4]

Input: "arg1 'arg 2' arg3 arg4"; Output: [arg1, arg 2, arg3, arg4]

My problem is in the second example. For when the argument is in quotation marks, I must keep it intact. And as I'm using PHP's explode function, I end up having two arguments ( [arg, 2] );

    
asked by anonymous 12.02.2016 / 11:35

2 answers

2

Assuming the default is arg being fixed:

$str = "arg1 'arg 2' arg3 arg4";
preg_match_all("~arg\d|'arg[^']+'~", $str, $matchs);

print_r($matchs[0]);

If you do not want to include ' use: arg\d|(?<=')arg[^']+(?=')

Issue

As commented arg are actually arguments of any kind:

So the regex had to be changed:

Input: seed.php app:init "app" "/seed/" "Lucas Mahle"
Regex: ['"].*?['"]|[^ ]+

    
12.02.2016 / 12:07
1

I do not know about PHP, but I created an example in javascript that might help you understand the regular expression (follow the comments in the code):

function splitArguments(arguments) {
  return arguments.replace(
      /* procura tudo que está entre ' nos argumentos */
      /\'([^']+)\'/g, function(replacement) {
        /* procura espaço no que está entre ' e substitui por '__' */
        return replacement.replace(/ /g, '__');
      })
    // troca os espaços em branco dos separadores por ||
    .replace(/ /g, '||')
    // retorna o __ para espaço em branco dentro do argumento
    .replace(/__/g, ' ')
    // aplica split pelo novo separador de argumentos ||
    .split(/\|\|/g);
}

console.log(splitArguments("arg1 arg2 arg3 arg4"));
console.log(splitArguments("arg1 'arg 2' arg3 arg4"));
console.log(splitArguments("arg1 'arg 2' arg3 'arg 4'"));
  

I think this update solved the flaws, if you notice any flaws in any test cases, please comment.

    
12.02.2016 / 12:27