首页 > 解决方案 > 匹配/提取两个字符串之间的所有字符

问题描述

我想从字符串中提取 John Doe\n*DRIVGo*\nVolledige naam: John Doe\nTelefoonnummer: 0612345678\nIP: 94.214.168.86\n

所以我猜正则表达式模式需要提取'Volledige naam:'和'\ n'之间的所有字符。有没有人可以帮助我?

标签: phpregex

解决方案


您可以使用此正则表达式来捕获组 1 中的名称,

naam:\s+([a-zA-Z ]+)

由于名称只能包含字母和空格,因此使用[a-zA-Z ]+字符集。

php示例代码,

$str = "\n*DRIVGo*\nVolledige naam: John Doe\nTelefoonnummer: 0612345678\nIP: 94.214.168.86\n";
preg_match('/naam:\s+([a-zA-Z ]+)/', $str, $matches);
print_r($matches[1]);

印刷,

John Doe

在线演示


推荐阅读