2

我想要一个正则表达式,它将多个事件捕获到一组中。例如,想象以下短语:

cat | likes her | mat
dog | goes to his | basket

我希望能够将短语的每个部分捕捉到一个固定的位置

array(
  0 => cat likes her mat
  1 => cat
  2 => likes her
  3 => mat
)

显然使用:

$regex = '/(cat|dog)( likes| goes| to| his| her)* (mat|basket)/';
preg_match($regex, "The cat likes her mat", $m);

给出:

array(
  0 => cat likes her mat
  1 => cat
  2 =>  likes
  3 =>  her
  4 => mat
)

但我总是想要 $m[3] 中的垫子/篮子,不管中间匹配了多少个单词。

我试过这个:

$regex = '/(cat|dog)(?:( likes| goes| to| his| her)*) (mat|basket)/';

试图阻止捕获多个子模式,但这只会导致第一个单词被捕获,即

array(
  0 => cat likes her mat
  1 => cat
  2 =>  likes
  3 => mat
)

有谁知道我如何捕捉整个短语的中间部分(病房长度未知),但仍然可以将其输入预测输出。

顺便说一句,我不能使用(cat|dog).*?(mat|basket),因为中间只允许指定的单词。

以上只是一个例子;每个子模式的实际用法都有更多选项。

谢谢。

4

2 回答 2

2

你试过这个模式吗:

/\b(cat|dog) ((?: ?(?:likes|goes|to|his|her)\b)*) ?(mat|basket)\b/
于 2013-05-12T17:24:02.717 回答
1

这个图案怎么样?

$regex = '/\b(cat|dog)\b((?:\b(?:\s+|likes|goes|to|his|her)\b)*)\b(mat|basket)\b/';
preg_match($regex, "The cat likes her mat", $m);

我有这个结果:

array (size=4)
  0 => string 'cat likes her mat' (length=17)
  1 => string 'cat' (length=3)
  2 => string ' likes her ' (length=11)
  3 => string 'mat' (length=3)

我投票支持 Casimir 的结果,但是他的模式在这些字符串上返回误报:

cat likesher mat
cat likes  her mat
cat mat
于 2013-05-12T17:25:15.553 回答