2013-05-02 154 views
-1

我需要提取一段字符串。我必须从system.cpu.util[,idle]net.if.out[eth2]分别提取idleeth2使用正则表达式从字符串中提取

我不知道如何检测纠正[] 有人可以帮我吗?

+1

手动你需要逃避[和],因为它们是元字符使用\\和\\] – RMcLeod 2013-05-02 14:13:27

回答

0

由于[和]是指在正则表达式中特殊的东西,所以必须在它们前面放一个\,否则preg_match将不会从字面上理解它们。与你的点相同。

+0

一个正则表达式像'preg_match('。* \ [\?P \ w + \]。*',$ test,$ matches);'应该工作,不应该吗?它没有。我错了什么? – Leitoo0 2013-05-02 14:21:09

+0

也不是:preg_match('\。* \ [\?P \ w + \] \。*',$ item-> name,$ matches);' – Leitoo0 2013-05-02 14:22:06

+0

@ Leitoo0:您需要分隔符:'preg_match('/ 。* \ [\(?P \ w +)\]。* /',$ test,$ matches);'和命名捕获组是'(?P 模式)' – nhahtdh 2013-05-02 14:23:03

0

这应该工作

preg_match('/\[,?(.*?)\]/', $test, $matches); 
1

这里是preg_match

var= net.if.out[eth2] 
preg_match('/\[,?(.*?)\]/', $var, $match); 
print match[1]; 
相关问题