描述
此表达式将匹配您的短语,并确保它们不会嵌入另一个更大的单词中。
^.*?(?:\s|^)(of\sthe\shouse|time|this\sis\show|home)(?=\W|$).*
PHP 代码示例:
你没有指定一种语言,所以我只是提供这个 php 示例来简单地展示它是如何工作的。
示例文本
1) "I was coming out of the house"
2) "I remember the time when I used to be a baby"
3) "Well, I am not sure what you did, but this is how I fix my problems"
4) "When are you coming home?"
5) "This is howard Timey said of the houseboat"
6) "The last word in this line is home
代码
<?php
$sourcestring="your source string";
preg_match_all('/^.*?(?:\s|^)(of\sthe\shouse|time|this\sis\show|home)(?=\W|$).*/imx',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
火柴
[0] => Array
(
[0] => 1) "I was coming out of the house"
[1] => 2) "I remember the time when I used to be a baby"
[2] => 3) "Well, I am not sure what you did, but this is how I fix my problems"
[3] => 4) "When are you coming home?"
[4] => 6) "The last word in this line is home
)
[1] => Array
(
[0] => of the house
[1] => time
[2] => this is how
[3] => home
[4] => home
)