Php 像linux命令一样的正则表达式

Php 像linux命令一样的正则表达式,php,regex,preg-replace,preg-match,Php,Regex,Preg Replace,Preg Match,因为它应该是一个正则表达式来实现这一点?想成为正则表达式来实现这一点吗 我的字符串: 值11-ttable-mtest-xtest2 我想要: (main value) value11 (-t value) = table (-m value) = test (-x value) = test2 也许可以添加更多的-y参数 谢谢不太清楚你在问什么。您可以使用 explode('-', 'value11-ttable-mtest-xtest2'); 为什么要使用遍历表达式?这看起来没有正则

因为它应该是一个正则表达式来实现这一点?想成为正则表达式来实现这一点吗

我的字符串:

值11-ttable-mtest-xtest2

我想要:

(main value) value11

(-t value) = table 
(-m value) = test
(-x value) = test2
也许可以添加更多的-y参数


谢谢

不太清楚你在问什么。您可以使用

explode('-', 'value11-ttable-mtest-xtest2');

为什么要使用遍历表达式?

这看起来没有正则表达式更容易求解。这里有一个建议:

$values = array();
foreach (explode('-', 'value11-ttable-mtest-xtest2') as $index => $string) {
    if ($index == 0) {
        $values['main'] = $string;
    } else {
        $option = substr($string, 0, 1);
        $value = substr($string, 1);
        $values[$option] = $value;
    }
}
这个正则表达式:
^([^-]*)(?:-([^-])([^-]+)*$
preg\u match\u all
应该可以工作

其中:

  • ([^-]*)
    :捕获任何字符的0到n倍,但-
  • -([^-])
    :捕获后面的第一个字母-
  • ([^-]+)
    :捕获任何字符的1到n倍,但-

感谢你做了这么多工作,我没想到会有一次简单的爆炸。