Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 匹配/提取两个字符串之间的所有字符_Php_Regex - Fatal编程技术网

Php 匹配/提取两个字符串之间的所有字符

Php 匹配/提取两个字符串之间的所有字符,php,regex,Php,Regex,我想从字符串\n*DRIVGo*\n colledige naam:John Doe\ntelefoonnumer:0612345678\nIP:94.214.168.86\n中提取John Doe 所以我想正则表达式模式需要提取'Volledige naam:'和'\n'之间的所有字符。有人能帮我吗 所需字符串始终存在于indexOf(':')处,并使用先前获得的indexOf值作为后续调用中的偏移量在同一调用处结束。(假设第一次调用并不表示找不到结果,也不表示发送调用的结果[这表示字符串中不

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


所以我想正则表达式模式需要提取'Volledige naam:'和'\n'之间的所有字符。有人能帮我吗

所需字符串始终存在于
indexOf(':')
处,并使用先前获得的
indexOf
值作为后续调用中的偏移量在同一调用处结束。(假设第一次调用并不表示找不到结果,也不表示发送调用的结果[这表示字符串中不包含完整的段])

为此使用正则表达式似乎不太有用,因为源字符串不会以某种需要自动机的方式变化

考虑一个简单的
split('\n')
操作[可以选择给定要获得的匹配长度],如果需要,可以在后面进行进一步的此类调用,以获得所需的值,而不需要任何底层引擎

所提供的逻辑将与Regex在其底层实现中为您所做的相同,尽管在内存和性能方面的相关成本通常仅在某些情况下是合理的[例如,涉及代码页或区域设置转换,但不限于,另一种情况是查找具有不正确变音、标点符号等的单词],在这种情况下,似乎不需要这些

考虑一个具有字段和方法的解析器构造,这些字段和方法可以获取[point to],并在需要时验证数据的完整性;这还允许您在大多数情况下快速序列化和反序列化结果

最后,由于您指出您的语言是PHP,我想我也应该让您知道,
indexOf
的等价物是
strpos
,下面的代码将演示在不使用regex的情况下解决此问题的各种方法

$str = "\n*DRIVGo*\nVolledige naam: John Doe\nTelefoonnummer: 0612345678\nIP: 94.214.168.86\n";
$search = chr(10);
$parts = explode($search, $str);
$partsCount = count($parts);
print_r($parts);
if($partsCount > 1) print($parts[1]); //*DRIVGo*

print('-----Same results via different methodology------');

$groupStart = 0;
$groupEnd = $groupStart; 
$max = strlen($str);

//While the groupEnd has not approached the length of str
while($groupEnd <= $max && 
      ($groupStart = strpos($str, $search, $groupStart)) >= 0 && // find search in str starting at groupStart, assign result to groupStart
      ($groupEnd = strpos($str, $search, $groupEnd + 1)) > $groupStart) // find search in str starting at groupEnd + 1, assign result to groupEnd
{   
    //Show the start, end, length and resulting substring
    print_r([$groupStart, $groupEnd, $groupEnd - $groupStart, substr($str, $groupStart, $groupEnd - $groupStart)]); 
    //advance the parsing
    $groupStart = $groupEnd;
}
$str=“\n*DRIVGo*\n收藏naam:John Doe\nTelefoonnumer:0612345678\nIP:94.214.168.86\n”;
$search=chr(10);
$parts=explode($search,$str);
$partscont=计数($parts);
印刷品(零件);
如果($partsCount>1)打印($parts[1]);//*DRIVGo*
打印('----通过不同的方法获得相同的结果---');
$groupStart=0;
$groupEnd=$groupStart;
$max=strlen($str);
//而groupEnd尚未接近str的长度
当($groupEnd=0&&//find search在str中从groupStart开始时,将结果分配给groupStart
($groupEnd=strps($str,$search,$groupEnd+1))>$groupStart)//在str中查找从groupEnd+1开始的搜索,将结果分配给groupEnd
{   
//显示开始、结束、长度和结果子字符串
打印([$groupStart,$groupEnd,$groupEnd-$groupStart,substr($str,$groupStart,$groupEnd-$groupStart)]);
//推进语法分析
$groupStart=$groupEnd;
}

所需字符串始终存在于
indexOf(“:”)
处,并在同一调用处结束,使用先前获得的
indexOf
值作为后续调用中的偏移量。(假设第一个调用不表示未找到结果,也不表示发送调用的结果。)[这表示字符串中不包含完整的段])

为此使用正则表达式似乎不太有用,因为源字符串不会以某种需要自动机的方式变化

考虑一个简单的
split('\n')
操作[可以选择给定要获得的匹配长度],如果需要,可以在后面进行进一步的此类调用,以获得所需的值,而不需要任何底层引擎

所提供的逻辑将与Regex在其底层实现中为您所做的相同,尽管在内存和性能方面的相关成本通常仅在某些情况下是合理的[例如,涉及代码页或区域设置转换,但不限于,另一种情况是查找具有不正确变音、标点符号等的单词],在这种情况下,似乎不需要这些

考虑一个具有字段和方法的解析器构造,这些字段和方法可以获取[point to],并在需要时验证数据的完整性;这还允许您在大多数情况下快速序列化和反序列化结果

最后,由于您指出您的语言是PHP,我想我也应该让您知道,
indexOf
的等价物是
strpos
,下面的代码将演示在不使用regex的情况下解决此问题的各种方法

$str = "\n*DRIVGo*\nVolledige naam: John Doe\nTelefoonnummer: 0612345678\nIP: 94.214.168.86\n";
$search = chr(10);
$parts = explode($search, $str);
$partsCount = count($parts);
print_r($parts);
if($partsCount > 1) print($parts[1]); //*DRIVGo*

print('-----Same results via different methodology------');

$groupStart = 0;
$groupEnd = $groupStart; 
$max = strlen($str);

//While the groupEnd has not approached the length of str
while($groupEnd <= $max && 
      ($groupStart = strpos($str, $search, $groupStart)) >= 0 && // find search in str starting at groupStart, assign result to groupStart
      ($groupEnd = strpos($str, $search, $groupEnd + 1)) > $groupStart) // find search in str starting at groupEnd + 1, assign result to groupEnd
{   
    //Show the start, end, length and resulting substring
    print_r([$groupStart, $groupEnd, $groupEnd - $groupStart, substr($str, $groupStart, $groupEnd - $groupStart)]); 
    //advance the parsing
    $groupStart = $groupEnd;
}
$str=“\n*DRIVGo*\n收藏naam:John Doe\nTelefoonnumer:0612345678\nIP:94.214.168.86\n”;
$search=chr(10);
$parts=explode($search,$str);
$partscont=计数($parts);
印刷品(零件);
如果($partsCount>1)打印($parts[1]);//*DRIVGo*
打印('----通过不同的方法获得相同的结果---');
$groupStart=0;
$groupEnd=$groupStart;
$max=strlen($str);
//而groupEnd尚未接近str的长度
当($groupEnd=0&&//find search在str中从groupStart开始时,将结果分配给groupStart
($groupEnd=strps($str,$search,$groupEnd+1))>$groupStart)//在str中查找从groupEnd+1开始的搜索,将结果分配给groupEnd
{   
//显示开始、结束、长度和结果子字符串
打印([$groupStart,$groupEnd,$groupEnd-$groupStart,substr($str,$groupStart,$groupEnd-$groupStart)]);
//推进语法分析
$groupStart=$groupEnd;
}

您可以使用此正则表达式捕获组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

您可以使用此正则表达式捕获组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

你可以