Php 是否从结果中排除正则表达式断言?
我试图从一个大型数据库中的产品描述中提取一部分文本(参考书) 例如:Php 是否从结果中排除正则表达式断言?,php,regex,regex-lookarounds,Php,Regex,Regex Lookarounds,我试图从一个大型数据库中的产品描述中提取一部分文本(参考书) 例如: books A 41.14 (products -> cat1 -> (1990-2000)) 我只需要a41.14 我曾在PHP中尝试过这一点(后面总是有括号) 但是它没有像预期的那样捕获,我需要排除断言,有可能吗?在正则表达式中使用\K来排除以前的匹配,并使用lookaheads来检查下面应该是\s*((即,零个或多个空格后跟(符号) PHP代码应该是 <?php $mystring = "books
books A 41.14 (products -> cat1 -> (1990-2000))
我只需要a41.14
我曾在PHP中尝试过这一点(后面总是有括号)
但是它没有像预期的那样捕获,我需要排除断言,有可能吗?在正则表达式中使用
\K
来排除以前的匹配,并使用lookaheads来检查下面应该是\s*(
(即,零个或多个空格后跟(
符号)
PHP代码应该是
<?php
$mystring = "books A 41.14 (products -> cat1 -> (1990-2000))";
$regex = '~books\s*\K.*?(?=\s*\()~';
if (preg_match($regex, $mystring, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> A 41.14
/=>A 41.14
如果您感兴趣的产品代码不能包含开括号字符,那么您可以简单地使用以下模式:
books\s+([^(]+)\s+\(
如果产品代码中可以出现开括号字符,则您的任务将更加棘手。请尝试以下操作:
preg_match( '/books\s?([\w]+\s?[\d|\.]+).*/i', "books A 41.14 (products -> cat1 -> (1990-2000))", $matches);
echo $matches[1];
希望有帮助
致以最诚挚的问候如果您在使用浮点数之前总是有一个字母
(\w (\d+)\.(\d+))
“books A 41.14”是否始终位于行的开头?格式是否始终相同:“books”+“大写字母”+“数字”?
preg_match( '/books\s?([\w]+\s?[\d|\.]+).*/i', "books A 41.14 (products -> cat1 -> (1990-2000))", $matches);
echo $matches[1];
(\w (\d+)\.(\d+))