2

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.

3

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

See also example 4 at php.net/manual/en/function.preg-match.php if you are looking for named subpatterns
Actually I need to match any logical operator, and ignore the quotations (single or double) if they surround the value, but thats basically what I'm looking for. Thanks
There are no "named capture groups" in this answer.
0

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

see 'subpatterns' and 'back references'

2 Comments

This link-only hint (answer) could have been a comment under the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.