我想帮助创建一个可以在句子中找到一组特定单词的正则表达式。在我们搜索句子之前,已知具体的单词或单词集。这些词总是存在于句子中。随着时间的推移,这套装置将会不断扩大。以下示例,
一组单词:“的房子”,“时间”,“这是如何”,“来< / strong>“,”主页“
应该返回匹配的句子:
1)“我出来了的房子” - &gt;匹配“的房子”
2)“我记得我曾经是个孩子的时间” - &gt;匹配“时间”
3)“好吧,我不确定你做了什么,但是这就是我解决问题的方式” - &gt;匹配“这是如何”
4)“你什么时候来回家?” - &GT;匹配“主页”
更新:实施语言将在PHP中
答案 0 :(得分:2)
此表达式将与您的短语匹配,并确保它们不会嵌入另一个较大的单词中。
^.*?(?:\s|^)(of\sthe\shouse|time|this\sis\show|home)(?=\W|$).*
你没有指定一种语言,所以我只是提供这个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
)