简体   繁体   English

正则表达式似乎有效,但不起作用

[英]regex seems to work but does not

I'm tring to get a function to parse the data from the source of a website: 我正试图获得一个功能来解析网站来源的数据:

function parse ($source) {
$res = array();
$matches = array();
$rx = "#^\s*routes\.push\(\{'start':([a-zA-Z0-9':,\-\s]*),\s'finish':([a-zA-Z0-9':,\-\s]*),\s'[a-zA-Z0-9':,\-\s]*':([0-9\s\-]){1,4},[a-zA-Z0-9':,\.\-\s]*}\);\s*$#";
preg_match_all($rx, $source, $matches);
    for($i=0,$_i=count($matches[1]);$i<$_i;++$i) {
    $res[$i][0]=$matches[1][$i];
    $res[$i][1]=$matches[2][$i];
    }

The code to be parsed is like : 要解析的代码如下:

routes.push({'start': tokyo, 'finish': LA, 'blue': 18, 'arcoffset': -20, 'transpacific': true});
    routes.push({'start': tokyo, 'finish': seattle, 'blue': 6, 'arcoffset': 5, 'transpacific': true});
    routes.push({'start': tokyo, 'finish': sanjose, 'blue': 12, 'arcoffset': -4, 'transpacific': true});
    routes.push({'start': amsterdam, 'finish': tokyo, 'blue': 5, 'green': 2, 'arcoffset': 20});

The problem is that nothing goes in the arrays. 问题在于数组中没有任何内容。 And when I check the regexp by doing echo preg_match_all($rx, $page, $matches); 当我通过执行echo preg_match_all($ rx,$ page,$ matches)检查正则表达式时;

it returns 0 So there might be a problem with my regexp, but according to Regex Tester, it's OK: 它返回0,所以我的regexp可能有问题,但是根据Regex Tester来看,没关系:

Check regex: http://regexpal.com?... 检查正则表达式:http://regexpal.com?

您似乎想要匹配3件事:2个城市和1个数字。

$rx = "\{'start':([a-zA-Z]*),\s'finish':([a-zA-Z]*),\s'[a-zA-Z]*':\s([0-9\s\-]{1,4})";

用^ end $并说:整个字符串都与该模式匹配。.甚至没有重复..尝试删除它们,因此使您的模式如下所示:

$rx = "/\s*routes\.push\(\{'start':([a-zA-Z0-9':,\-\s]*),\s'finish':([a-zA-Z0-9':,\-\s]*),\s'[a-zA-Z0-9':,\-\s]*':([0-9\s\-]){1,4},[a-zA-Z0-9':,\.\-\s]*}\);\s*/";

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

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