Split arguments with regex

Asked

Viewed 76 times

0

I have difficulties with regex. I just need to split an input string.

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. Because when the argument is in quotes, I need to keep it integrated. And as I’m using the PHP explodes function, I end up having two arguments ([arg, 2]);

  • Is php via command line?

2 answers

2


Assuming the pattern 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[^']+(?=')

Editing

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 : ['"].*?['"]|[^ ]+

  • It’s not fixed. What would it look like? An example of input: 'Seed.php app:init "app" "/Seed/" "Lucas Mahle'

  • @lucasDotWith this sort of thing should be specified when talking about regex. Just to know you can open with single quote and close with double and vise versa?

  • @lucasDotCom Editei, see if you are now in agreement.

  • William, it worked perfectly. That’s all I needed.

1

I’m not familiar with 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 believe this update solved the flaws, if you observe any flaw in any test case comment.

  • Thank you Fernando, your code served me for another problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.