如何使用正则表达式从以下字符串中提取数字和键?
Duplicate entry '24040446669881344' for key 'clave_something'
Duplicate entry '24040446669881345' for key 'clave_something_else'
我尝试使用子字符串,但当链的长度改变时它不起作用。
谢谢!
最佳答案
有了这个,您将从字符串中获取所有整数:
preg_match_all('~\d+~', $string, $match);
print_r($match);
demo
对于您的具体示例,还要提取 key :
preg_match("~'(.+?)'.+'(.+?)'~", $string, $match);
print_r($match);
demo
或者如果你想获取多行字符串:
preg_match_all("~'(.+?)'.+'(.+?)'~m", $string, $match, PREG_SET_ORDER);
print_r($match);
demo
关于php - 使用正则表达式从字符串中提取模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796009/