Php PCRE使用斜杠和行尾分隔的捕获组进行后匹配(查找)
我的目标是一个正则表达式,它捕获以下字符串中propertyComplexity之后的{type}和{code}:-Php PCRE使用斜杠和行尾分隔的捕获组进行后匹配(查找),php,regex,pcre,Php,Regex,Pcre,我的目标是一个正则表达式,它捕获以下字符串中propertyComplexity之后的{type}和{code}:- /json/score/propertyComplexity/{type}/{code} {type}和{code}是变量,可以是任何东西,也可以是零,例如 /json/score/propertyComplexity 我从以下表达式开始:- (?<=propertyComplexity)\/(.*)\/|$ 应产生4个匹配/捕获组;{type}、{code}、{pa
/json/score/propertyComplexity/{type}/{code}
{type}和{code}是变量,可以是任何东西,也可以是零,例如
/json/score/propertyComplexity
我从以下表达式开始:-
(?<=propertyComplexity)\/(.*)\/|$
应产生4个匹配/捕获组;{type}、{code}、{param3}、{param4}
如果有帮助,可以在WADL中处理@path属性的上下文中使用。捕获的内容实际上是不相关的,因为将使用匹配计数(与确定调用哪个WADL资源时传递的参数计数进行比较)
URL有效性不是一项要求
当WADL中的参数总是封装在{param}占位符中时,不需要在斜杠上或斜杠后进行匹配
因此,我只是在preg_match_all()==count($this->passedArgs)中使用以下表达式
谢谢大家的贡献。答案将授予Jaytea。这里有一个将正则表达式与
explode()
相结合的解决方案,因为我非常确定,使用PCRE无法单独捕获(或计数)重复组
正则表达式模式要求在/propertyComplexity
之后的任何/
分隔的段(我在斜杠前面加了一点限制)都是非空的,因此允许任何非空的内容,而不仅仅是像{type}
这样被大括号包围的内容
该模式可能比它需要的要复杂一些,但它使分解结果更加简短(无需修剪斜线)
实际的参数值将在数组$arguments
中,但为了更加简短,我省略了在结果中显示这些值
$urls = array(
'/json/score/propertyComplexity',
'/json/score/propertyComplexity/',
'/json/score/propertyComplexity//', // invalid
'/json/score/propertyComplexity/{type}',
'/json/score/propertyComplexity/{type}/',
'/json/score/propertyComplexity/{type}//', // invalid
'/json/score/propertyComplexity/{type}/{code}',
'/json/score/propertyComplexity/{type}/{code}/{param3}',
'/json/score/propertyComplexity/{type}/{code}/{param3}/{param4}',
'/json/score/propertyComplexity/{type}/{code}/{param3}/{param4}/{param5}'
);
foreach( $urls as $url ) {
printf( 'testing %s' . PHP_EOL, $url );
if( preg_match( '~(?<=/propertyComplexity)(?:/(?<arguments>[^/]+(/[^/]+)*))?(?:/?$)~', $url, $matches ) ) {
$arguments = isset( $matches[ 'arguments' ] ) ? explode( '/', $matches[ 'arguments' ] ) : array();
printf( ' URL is valid: argument count is %d' . PHP_EOL, count( $arguments ) );
}
else {
echo ' URL is invalid' . PHP_EOL;
}
echo PHP_EOL;
}
您可以将
preg\u match\u all
与以下表达式结合使用,以实现所需的功能:
/(?(?=^).*?propertyComplexity(?=(?:\/[^\/]+)*\/?$)|\G)\/\K([^\/]+)/
“\G”断言在主题的第一个位置匹配,在本例中,这实际上意味着它在最后一个匹配结束的位置匹配。这里的基本策略是在开始时验证字符串的格式,然后在随后的几轮中一次捕获一个属性
我不确定您对验证的要求有多严格,所以我把它保持得很简单。我不认为
{type}
和{code}
中接受换行符,或者它们是吗?换行符或其他特殊字符不会出现在斜杠之间,因为斜杠之间的变量将构成URL的一部分?谢谢Gurnam。虽然不太合适。更新了帖子,使其更加具体。
$urls = array(
'/json/score/propertyComplexity',
'/json/score/propertyComplexity/',
'/json/score/propertyComplexity//', // invalid
'/json/score/propertyComplexity/{type}',
'/json/score/propertyComplexity/{type}/',
'/json/score/propertyComplexity/{type}//', // invalid
'/json/score/propertyComplexity/{type}/{code}',
'/json/score/propertyComplexity/{type}/{code}/{param3}',
'/json/score/propertyComplexity/{type}/{code}/{param3}/{param4}',
'/json/score/propertyComplexity/{type}/{code}/{param3}/{param4}/{param5}'
);
foreach( $urls as $url ) {
printf( 'testing %s' . PHP_EOL, $url );
if( preg_match( '~(?<=/propertyComplexity)(?:/(?<arguments>[^/]+(/[^/]+)*))?(?:/?$)~', $url, $matches ) ) {
$arguments = isset( $matches[ 'arguments' ] ) ? explode( '/', $matches[ 'arguments' ] ) : array();
printf( ' URL is valid: argument count is %d' . PHP_EOL, count( $arguments ) );
}
else {
echo ' URL is invalid' . PHP_EOL;
}
echo PHP_EOL;
}
testing /json/score/propertyComplexity
URL is valid: argument count is 0
testing /json/score/propertyComplexity/
URL is valid: argument count is 0
testing /json/score/propertyComplexity//
URL is invalid
testing /json/score/propertyComplexity/{type}
URL is valid: argument count is 1
testing /json/score/propertyComplexity/{type}/
URL is valid: argument count is 1
testing /json/score/propertyComplexity/{type}//
URL is invalid
testing /json/score/propertyComplexity/{type}/{code}
URL is valid: argument count is 2
testing /json/score/propertyComplexity/{type}/{code}/{param3}
URL is valid: argument count is 3
testing /json/score/propertyComplexity/{type}/{code}/{param3}/{param4}
URL is valid: argument count is 4
testing /json/score/propertyComplexity/{type}/{code}/{param3}/{param4}/{param5}
URL is valid: argument count is 5
/(?(?=^).*?propertyComplexity(?=(?:\/[^\/]+)*\/?$)|\G)\/\K([^\/]+)/