php regex named groups

六眼飞鱼酱① 提交于 2019-12-11 01:35:31

问题


can someone tell me how to use named groups syntax in PHP?

I'm trying to parse a simple math equation, for example someVariable!=someValue.
I'd like to get 3 values from matching operation, stored in 3 variable variable, operator, value.


回答1:


Is this basically what you're looking for?

$equation = 'someVariable!=someValue';
$matches = array();
preg_match('~^(\w+)([!=]+)(\w+)$~', $equation, $matches);

$variable = $matches[1];
$operator = $matches[2];
$value = $matches[3];

The actual regular expression is pretty silly, but I assume you already have that part figured out.




回答2:


http://php.net/manual/en/reference.pcre.pattern.syntax.php

see 'subpatterns' and 'back references'



来源:https://stackoverflow.com/questions/5109424/php-regex-named-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!