說明
這個表達式將匹配您的詞組,並保證它們不會嵌入到另一個更大的詞。
^.*?(?:\s|^)(of\sthe\shouse|time|this\sis\show|home)(?=\W|$).*
![enter image description here](https://i.stack.imgur.com/X3pp3.png)
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
)
房子的簡單組合'「|添e |這是應該如何工作,這是什麼問題? – dasblinkenlight
什麼是風味/工具? – acdcjunior
@acdcjunior可能是PHP,即PCRE(由於'preg-match'標籤) –