Regex—匹配特定数字前面的一个单词
一 需求
匹配到1前面并且是'='号分隔的单词
<?php
$patten = "/\b(\w+)\b\s*[^d][^and]?\s*\\?3/";
$conditionString = 'rarity = ?1 exp_start <= ?2 and exp_end > ?3';
$matches = array();
preg_match($patten, $conditionString, $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);
?>
运行结果
二 需求升级
不只是‘=’号,还可以是 匹配到‘=’ 或者‘>’或者‘<=’等,这样1,2和3都可以匹配
<?php
$patten = "/\b(\w+)\b\s*[^d][^and]?\s*\\?3/";
$conditionString = 'rarity = ?1 exp_start <= ?2 and exp_end > ?3';
$matches = array();
preg_match($patten, $conditionString, $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);
?>
运行结果
三 分析总结
参考:
http://php.net/manual/en/function.preg-match.php
PREG_OFFSET_CAPTURE