从左侧开始使用正则表达式(即,两侧可能的最窄匹配)

时间:2022-08-22 12:16:19

Let's say I'm trying to match /dog.*lab/ against this text:

假设我正在尝试将/dog.*lab/与此文本匹配:

"I have a dog. My dog is a black lab. He was created in a laboratory."

“我有一只狗。我的狗是一个黑人实验室。他是在实验室里创造的。”

Greedily, it would match "dog. My dog is a black lab. He was created in a lab".

贪婪地,它会匹配“狗。我的狗是一个黑色的实验室。他是在实验室里创造的”。

I want to find the matches that are narrowest from both sides. If I use the ungreedy modifier like
/dog.*?lab/ or /dog.*lab/U it will match less but still too much:
"dog. My dog is a black lab"

我想找到两边最窄的比赛。如果我使用像/dog.*?lab/或/dog.*lab/U这样的ungreedy修饰符,它会匹配更少但仍然太多:“狗。我的狗是一个黑色实验室”

Is there a way to make my search ungreedy from the left also, thus matching only "dog is a black lab"?

有没有办法从左边开始搜索我的搜索,因此只匹配“狗是黑实验室”?

Much thanks. Sorry for the contrived example.

非常感谢。抱歉这个人为的例子。

3 个解决方案

#1


10  

You could use a look-ahead assertion that excludes the occurrence of dog between dog and lab:

您可以使用前瞻性断言来排除狗与实验室之间狗的发生:

/dog(?:(?!dog).)*?lab/

#2


2  

This works for me:

这对我有用:

$str = "I have a dog. My dog is a black lab. He was created in a laboratory.";
if(preg_match('/.*(dog.*?lab)/',$str,$m)) {
    var_dump($m);
}

#3


1  

An idea might be to try to use a negated character set, like [^.!?], which would match all characters except ., ? and !, and therefore you can be sure that it is within the same sentence:

一个想法可能是尝试使用否定的字符集,如[^。!?],它将匹配除。,?之外的所有字符。和!,因此你可以确定它在同一句话内:

$string = "I have a dog. My dog is a black lab. He was created in a laboratory.";
preg_match('/dog[^.!?]*?lab/', $string, $match);
echo $match[0]; // Echoes "dog is a black lab"

#1


10  

You could use a look-ahead assertion that excludes the occurrence of dog between dog and lab:

您可以使用前瞻性断言来排除狗与实验室之间狗的发生:

/dog(?:(?!dog).)*?lab/

#2


2  

This works for me:

这对我有用:

$str = "I have a dog. My dog is a black lab. He was created in a laboratory.";
if(preg_match('/.*(dog.*?lab)/',$str,$m)) {
    var_dump($m);
}

#3


1  

An idea might be to try to use a negated character set, like [^.!?], which would match all characters except ., ? and !, and therefore you can be sure that it is within the same sentence:

一个想法可能是尝试使用否定的字符集,如[^。!?],它将匹配除。,?之外的所有字符。和!,因此你可以确定它在同一句话内:

$string = "I have a dog. My dog is a black lab. He was created in a laboratory.";
preg_match('/dog[^.!?]*?lab/', $string, $match);
echo $match[0]; // Echoes "dog is a black lab"