使用正则表达式 PHP 搜索字符串


Searching a String using Regular Expression PHP

我有一个字符串,我需要搜索这个字符串并能够将地址详细信息分配给一个变量:

它在字符串中的外观:

infoone:"infoone"infotwo:"infotwo"address:"123 fake street pretend land"infothree:"infothree"infofour:"infofour" address:"345 fake street pretend land"infofive: "infofive"infosix: "infosix"

我将如何使用正则表达式搜索此字符串以仅提升单词地址后面倒逗号中的数据?

注意:我不能针对短语"123假街假装土地",因为这只是倒逗号中可能使用的示例。

是一个很好的正则表达式

^address:"([^"]*)

这是在带有选项的 php 中,以便 ^ 在行首匹配,我们抬出组 1

preg_match_all('/^address:"([^"]*)/m', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[1];

更新 1

preg_match_all('/^address:"([^"]*)/m', $subject, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
    for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
        # Matched text = $result[$matchi][$backrefi];
    } 
}

更新 2

使用新的示例输入,只需在开头的 ^ 处保留,这样它就变成了

address:"([^"]*)

使用此正则表达式

$str='infoone:"infoone"infotwo:"infotwo"address:"123 fake street pretend land"infothree:"infothree"infofour:"infofour" address:"345 fake street pretend land"infofive: "infofive"infosix: "infosix"';
preg_match_all("/address:'"(.*)'"/siU",$str,$out);
print_r($out[1]);

好吧,正则表达式本身将是^address:"(.*)"$的。

显然,您需要添加相关的preg_match()调用。