获取与正则表达式匹配的不完整单词
Get incomplete word matched with regular expression
我有一段文字,我需要找到以一个词开头的词,我需要得到与这个表达式匹配的完整词。
示例:
我在如下文本中搜索 "abc"
:"Hi abc is abc123"
我需要得到一个数组:[0] = "abc" and [1] = "abc123"
因为两者都与我的表达式匹配 "abc"
.
我有这个代码:
$words = [];
foreach($items as $i){
$text = $l->getText();
preg_match('*'."#".$q.'*', $text, $words[]);
}
但我总是[0] = "abc" and [1] = "abc"
。
我该怎么做?
//Regular expresson to find: 'abc', followed by any
//characters, until a whitespace character
$re = "/(abc[^\s]*)/";
//String to search
$str = "Hi abc is abc123";
//Find all matches (/g)
preg_match_all($re, $str, $matches);
//Show matches in array
print_r($matches[0]);
将输出:
Array ( [0] => abc [1] => abc123 )
我有一段文字,我需要找到以一个词开头的词,我需要得到与这个表达式匹配的完整词。
示例:
我在如下文本中搜索 "abc"
:"Hi abc is abc123"
我需要得到一个数组:[0] = "abc" and [1] = "abc123"
因为两者都与我的表达式匹配 "abc"
.
我有这个代码:
$words = [];
foreach($items as $i){
$text = $l->getText();
preg_match('*'."#".$q.'*', $text, $words[]);
}
但我总是[0] = "abc" and [1] = "abc"
。
我该怎么做?
//Regular expresson to find: 'abc', followed by any
//characters, until a whitespace character
$re = "/(abc[^\s]*)/";
//String to search
$str = "Hi abc is abc123";
//Find all matches (/g)
preg_match_all($re, $str, $matches);
//Show matches in array
print_r($matches[0]);
将输出:
Array ( [0] => abc [1] => abc123 )