Pregex 不匹配正确的模式
Pregex is not matching the correct pattern
当我尝试用正则表达式匹配一个字符串时,它不起作用。但是,据我所知,我认为正则表达式规则很好。谁能帮我找出我的表情中缺少什么。
匹配样本
- HAD-ORAAS0001545-ORBTD0003457
- HAD-ORBTD0001545-ORAAS0003457
<?php
print_r((preg_match('/^HAD-OR[AAS,BTD][0-9]{5,7}$/','HAD-ORBTD0009999',$m)));
?>
根据您展示的示例,请您尝试以下操作。在线正则表达式演示:Online regex demo
^HAD(?:-OR(?:AAS|BTD)[0-9]{5,7}){1,2}$
说明: 为以上添加详细说明。
^HAD ##Checking if it starts with HAD.
(?: ##Starting non capturing group from here.
-OR ##Matching -OR string here.
(?: ##Starting a non-capturing group here.
AAS|BTD ##Matching either AAS OR BTD here.
) ##Closing non-capturing group here.
[0-9]{5,7} ##Putting digits should come with occurrences of 5 to 7 here.
) ##Closing very first non capturing group here.
{1,2}$ ##Mentioning that either it should be 1 or 2 occurrences of whole first non capturing group.
当我尝试用正则表达式匹配一个字符串时,它不起作用。但是,据我所知,我认为正则表达式规则很好。谁能帮我找出我的表情中缺少什么。
匹配样本
- HAD-ORAAS0001545-ORBTD0003457
- HAD-ORBTD0001545-ORAAS0003457
<?php
print_r((preg_match('/^HAD-OR[AAS,BTD][0-9]{5,7}$/','HAD-ORBTD0009999',$m)));
?>
根据您展示的示例,请您尝试以下操作。在线正则表达式演示:Online regex demo
^HAD(?:-OR(?:AAS|BTD)[0-9]{5,7}){1,2}$
说明: 为以上添加详细说明。
^HAD ##Checking if it starts with HAD.
(?: ##Starting non capturing group from here.
-OR ##Matching -OR string here.
(?: ##Starting a non-capturing group here.
AAS|BTD ##Matching either AAS OR BTD here.
) ##Closing non-capturing group here.
[0-9]{5,7} ##Putting digits should come with occurrences of 5 to 7 here.
) ##Closing very first non capturing group here.
{1,2}$ ##Mentioning that either it should be 1 or 2 occurrences of whole first non capturing group.