I have to split a command string into segments using regex. I am looking for a very basic parser to create some custom functions, for example I have this command:
rm --remove all --keep some --but-not *.php --or-like "?-imp-*.*"
Now I want to split this string into multiple segments each containing the argument name and value, e.g.
rm
--remove all
--keep some
--but-not *.php
--or-like "?-imp-*.*"
So I can further split each segment off blank space and have the argument name and value separated.
I am not good at RegEx. So far I've written this Regex to extract the argument and value part only but it does not match the words at end of string or those with special characters like * and ?
Regex
(?<=\s)--([^--]*)(?=(\s--))
and then I grab the name of command by
(^\w+)
Any thought on this ?
--with the next element (if the next element doesn't start with--).