[英]PHP preg_match part of string efficiently
For example, given the following function definition: 例如,给定以下函数定义:
function match($subject, $pattern, $offset);
These tests should succeed: 这些测试应该成功:
$subject = "23 is a number, so is 10";
match($subject, '[0-9]+', 0) == '23';
match($subject, '[0-9]+', 3) == false;
match($subject, '[0-9]+', 6) == false;
match($subject, '[0-9]+', 8) == false;
match($subject, '[a-z]+', 0) == false;
match($subject, '[a-z]+', 3) == 'is';
match($subject, '[a-z]+', 6) == 'a';
match($subject, '[a-z]+', 8) == 'number';
One possible way is to match a substring starting at $offset
by using ^
: 一种可能的方法是使用^
匹配以$offset
开头的子字符串:
function match($subject, $pattern, $offset) {
if (preg_match("/^($pattern)/", substr($subject, offset), $matches)) {
return $matches[1];
}
return false;
}
This would make a copy of a the string, which ofcourse isn't efficient for large strings. 这将创建一个字符串的副本,这当然对于大字符串而言效率不高。
Another possible way to implement match
would be: 实现match
另一种可能方法是:
function match($subject, $pattern, $offset) {
if (preg_match("/($pattern)/", $subject, $matches, PREG_OFFSET_CAPTURE, $offset)) {
if ($matches[1][1] == $offset) {
return $matches[1][0];
}
}
return false;
}
But this would keep trying to match even if the first character mismatches. 但是,即使第一个字符不匹配,也会继续尝试匹配。
Question : How can I match a part of a string efficiently? 问题 :如何有效地匹配字符串的一部分?
Or maybe better, is it possible to assert the offset position? 也许更好,是否可以声明偏移位置? Like ^
would assert the start of the string. 就像^
会断言字符串的开头。
You can try something like this: 您可以尝试如下操作:
function match($subject, $pattern, $offset) {
if (preg_match('/^.{' . $offset . '}\K' . $pattern . '/us', $subject, $match))
return $match[0];
return false;
}
Better! 更好! You can use the \\G
anchor that matches at the offset: 您可以使用在偏移处匹配的\\G
锚:
function match($subject, $pattern, $offset) {
if (preg_match('/\G' . $pattern . '/us', $subject, $match, 0, $offset))
return $match[0];
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.