php正则表达式命名组 [英] php regex named groups

查看:64
本文介绍了php正则表达式命名组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何在 PHP 中使用命名组语法?

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

我正在尝试解析一个简单的数学方程,例如 someVariable!=someValue.
我想从匹配操作中获得 3 个值,存储在 3 个变量 variableoperatorvalue 中.

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.

推荐答案

这基本上是您要找的吗?

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.

这篇关于php正则表达式命名组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆