ZolaKt
ZolaKt

Reputation: 4721

php regex named groups

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.

Upvotes: 2

Views: 3514

Answers (2)

koen
koen

Reputation: 13737

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

see 'subpatterns' and 'back references'

Upvotes: 0

sdleihssirhc
sdleihssirhc

Reputation: 42496

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.

Upvotes: 1

Related Questions