来自mysql错误消息的正则表达式列名

时间:2013-02-26 13:46:57

标签: php

我想从以下错误消息中删除第二个'

Duplicate entry 'this_can_be_anything' for key 'I_want_to_grab_this'

我正在使用php,但对于正则表达式如何工作却非常模糊。或者也许我应该使用其他东西而不是正则表达式?任何方向? TY。

2 个答案:

答案 0 :(得分:1)

直接的:

preg_match('/Duplicate entry \'.+\' for key \'(.+)\'/', $s, $m);并使用$m[1]

$m

array(2) {
  [0]=>
  string(68) "Duplicate entry 'this_can_be_anything' for key 'I_want_to_grab_this'"
  [1]=>
  string(19) "I_want_to_grab_this"
}

答案 1 :(得分:0)

这应该有效:

$yourstring = 'Duplicate entry...';
if (preg_match("=^[^']*'[^']*'[^']*'([^']*)'=", $yourstring, $matches)) {
    echo "Found value: " . $matches[1];
}

但是,这将匹配格式为

的任何行
Something 'something' something 'something'

不确定你是否想要那个。如果没有,请按照answer by k102