简体   繁体   English

获取不完整的单词与正则表达式匹配

[英]Get incomplete word matched with regular expression

I have a text and I need to find words which begens with one word, and I need to get the complete word which match with this expression. 我有一个文本,我需要找到一个单词衍生的单词,并且需要获取与此表达式匹配的完整单词。

Example: I search "abc" in a text like: "Hi abc is abc123" and I need to get an array with: [0] = "abc" and [1] = "abc123" because both match with my expresion "abc" . 示例:我在诸如"Hi abc is abc123"的文本中搜索"abc" ,并且我需要获取一个数组: [0] = "abc" and [1] = "abc123"因为两者都与我的表达式"abc"匹配"abc"

I have this code: 我有以下代码:

$words = [];
    foreach($items as $i){
        $text = $l->getText();
        preg_match('*'."#".$q.'*', $text, $words[]);
    }

But I get always [0] = "abc" and [1] = "abc" . 但是我总是得到[0] = "abc" and [1] = "abc"

How can I do this? 我怎样才能做到这一点?

//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]);

Will output: 将输出:

Array ( [0] => abc [1] => abc123 ) 数组([0] => abc [1] => abc123)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM