我想从以下错误消息中删除第二个'
Duplicate entry 'this_can_be_anything' for key 'I_want_to_grab_this'
我正在使用php,但对于正则表达式如何工作却非常模糊。或者也许我应该使用其他东西而不是正则表达式?任何方向? TY。
答案 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。