使用preg_match查找文本中短语的非贪婪匹配


find not greedy matches of phrase in text with preg_match

我想在文档中搜索一个短语(在这种情况下是长尾关键字),但希望在搜索的单词之间允许空格/单词。

例如,我的文本是:

"ShinyTeeth inc .是一家位于田纳西州查塔努加的优秀牙科诊所。"

我在找"牙科诊所查塔努加田纳西州"

"CrookedTeeth inc是一家优秀的牙科诊所位于田纳西州查塔努加。"

我需要一个正则表达式,可以在文本中找到我的关键字,每个关键字之间有X空间/单词的允许。在这种情况下,例如,最多两个单词的间距,以便它可以识别我的关键字在我的文本中找到。

Thanks in advance

如果您试图匹配多个短语,那么在PHP中使用Regex匹配字符串文字可能会很困难。最好在数据库上使用MySQL全文搜索来完成这类事情。话虽如此,这里有一些字符串和一些我正在测试的正则表达式模式。

<?php
$strings= array('Chattanooga Tennessee dental practice.',
"ShinyTeeth inc is an excellent dental clinic based in Chattanooga, Tennessee.",
"My dentist SmileyTeeth in chattanooga tennessee has the coolest practice.",
"And I'm looking for '"dental clinic chattanooga tennessee'"",
"CrookedTeeth inc is an excellent dental clinic located in Chattanooga, Tennessee.");
$patterns = array(
'!((dental office|dentist|dental practice|dental clinic)[^'s.]?'s+([^'s.]+'s?){0,2}'s+[^'s.]?(Chattanooga(,)?)? (Tennessee)?)!is',
'!((dental|dentist|doctor)'s+(clinic|practice|office)'s+([^'s]+'s?){0,2}'s+(Chattanooga(,)?)? (Tennessee)?)!is',
);
foreach($strings as $string){
    foreach($patterns as $pattern){
        if(preg_match($pattern,$string)){
            echo "'n`$pattern` '"matches'"'n $string'n";
        } else {
            echo "'n`$pattern` '"does not match'"'n $string'n";         
        }
    }
}
?>

!((dental office|dentist|dental practice|dental clinic)[^'s.]?'s+([^'s.]+'s?){0,2}'s+[^'s.]?(Chattanooga(,)?)? (Tennessee)?)!is"不匹配"查塔努加田纳西州牙科练习。

!((dental|dentist|doctor)'s+(clinic|practice|office)'s+([^'s]+'s?){0,2}'s+(Chattanooga(,)?)? (Tennessee)?)!is "不匹配" Chattanooga Tennessee dental "练习。

!((dental office|dentist|dental practice|dental clinic)[^'s.]?'s+([^'s.]+'s?){0,2}'s+[^'s.]?(Chattanooga(,)?)? (Tennessee)?)!is"匹配"ShinyTeeth inc是一家优秀的牙科公司位于田纳西州查塔努加的诊所。

!((dental|dentist|doctor)'s+(clinic|practice|office)'s+([^'s]+'s?){0,2}'s+(Chattanooga(,)?)? (Tennessee)?)!is"matches"ShinyTeeth inc是一家优秀的牙科公司位于田纳西州查塔努加的诊所。

!((dental office|dentist|dental practice|dental clinic)[^'s.]?'s+([^'s.]+'s?){0,2}'s+[^'s.]?(Chattanooga(,)?)? (Tennessee)?)!is "匹配"我在查塔努加的牙医SmileyTeeth田纳西州有最酷的练习。

!((dental|dentist|doctor)'s+(clinic|practice|office)'s+([^'s]+'s?){0,2}'s+(Chattanooga(,)?)? (Tennessee)?)!is "不匹配" My dental SmileyTeeth in田纳西州查塔努加市有最酷的练习。

!((dental office|dentist|dental practice|dental clinic)[^'s.]?'s+([^'s.]+'s?){0,2}'s+[^'s.]?(Chattanooga(,)?)? (Tennessee)?)!is "does not match" And I'm looking for "dental clinic .查塔努加田纳西州"

!((dental|dentist|doctor)'s+(clinic|practice|office)'s+([^'s]+'s?){0,2}'s+(Chattanooga(,)?)? (Tennessee)?)!is "does not match" And I'm looking for "dental clinic查塔努加田纳西州"

!((dental office|dentist|dental practice|dental clinic)[^'s.]?'s+([^'s.]+'s?){0,2}'s+[^'s.]?(Chattanooga(,)?)? (Tennessee)?)!is "matches" CrookedTeeth inc是一名优秀的牙科医生位于田纳西州查塔努加的诊所。

!((dental|dentist|doctor)'s+(clinic|practice|office)'s+([^'s]+'s?){0,2}'s+(Chattanooga(,)?)? (Tennessee)?)!is "matches" CrookedTeeth inc是一名优秀的牙科医生位于田纳西州查塔努加的诊所。