开发者

php regex named groups

开发者 https://www.devze.com 2023-02-13 10:58 出处:网络
can someone tell me how to use named groups syntax in PHP? I\'m tryingto parse a simple math equ开发者_如何学Goation, for example someVariable!=someValue.

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

I'm trying to parse a simple math equ开发者_如何学Goation, for example someVariable!=someValue.

I'd like to get 3 values from matching operation, stored in 3 variable variable, operator, value.


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.


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

see 'subpatterns' and 'back references'

0

精彩评论

暂无评论...
验证码 换一张
取 消