0

因为它应该是一个正则表达式来做到这一点?想成为一个正则表达式来做到这一点?

我的字符串:

value11-ttable-mtest-xtest2

我想要:

(main value) value11

(-t value) = table 
(-m value) = test
(-x value) = test2

并且可能添加更多 -y 参数

谢谢

4

3 回答 3

1

这看起来在没有正则表达式的情况下更容易解决。这里有一个建议:

$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;
    }
}
于 2013-10-31T10:06:52.757 回答
0

你在这里问什么不是很清楚。你可以得到你描述使用的结果

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

为什么要使用 ergular 表达式?

于 2013-10-31T10:04:43.123 回答
0

这个正则表达式 : ^([^-]*)(?:-([^-])([^-]+))*$withpreg_match_all应该可以工作。

在哪里 :

  • ([^-]*): 捕获 0 到 n 次任何字符,但 -
  • -([^-]): 在 - 之后捕获第一个字母
  • ([^-]+):捕获任何字符的 1 到 n 次,但 -
于 2013-10-31T10:16:06.317 回答