0

我需要获取 id,我有一些街道或道路或广场的名称以及一些地区或地方的名称......所以我需要从我有 word1 和 word2 的字符串中获取 id。

我正在尝试这个:

<?php
$pattern = '~^(?<id>\\S)\\t+[name1qaz]+[\\w\'-]+[\\w\'-]+[name2asd]+[\\w\'-]~mi';
$subject = '1 name1qaz avenue (name2qwe region) 
2 name1wsx road (name2asd region) 
3 name1edc street (name2zxc region) 
4 name1qaz square name2asd place 
5 name1wsx avenue (name2qwe region) 
7 name1edc street (name2zxc place) 
8 name1qaz road name2zxc region 
9 name1wsx square (name2asd region)';
$result = preg_match( $pattern, $subject , $matches );
echo $result;
print_r($matches);
?>

结果我收到空数组

4

2 回答 2

0

你的正则表达式是错误的。用这个:

$pattern = '~^(?<id>\d+)\s+name1qaz\s+(?=.*?name2asd\b)~mi';

现场演示:http: //ideone.com/g4qxDV

于 2013-06-25T17:10:41.237 回答
0
$pattern = '~([0-9]+)[ ]?([a-zA-Z0-9]+)[ ]?([a-zA-Z0-9]+)[ ]?\(([a-zA-Z0-9]+)[ ]?([a-zA-Z]+)\)~';

preg_match_all($patterm, $subject, $matches);

print_r($matches);

我有点迷失了阅读你的模式,所以我写了我会做什么来匹配你的字符串中的行。对于您是否希望将所有信息作为单独的值返回,我并不是 100%,因此我在结果数组的空格之间包含了所有单词/数字。

于 2013-06-25T17:11:22.130 回答