php - Pregex 不匹配正确的模式
问题描述
当我尝试将字符串与正则表达式匹配时,它不起作用。但是我认为正则表达式规则对我的知识来说很好。谁能帮我确定我的表达中缺少什么。
样品匹配
- HAD-ORAAS0001545-ORBTD0003457
- HAD-ORBTD0001545-ORAAS0003457
<?php
print_r((preg_match('/^HAD-OR[AAS,BTD][0-9]{5,7}$/','HAD-ORBTD0009999',$m)));
?>
解决方案
使用您显示的示例,您能否尝试以下操作。在线正则表达式演示:在线正则表达式演示
^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.
推荐阅读
- javascript - 试图让 momentjs 计算时间差
- java - Arraylist 和 Jframe
- excel - 如何删除文本字符串中单词的最后一个字符并在 Excel 中使用 VBA 插入另一个单元格?
- cassandra - Cassandra Nodetool Listsnapshots 输出不清楚
- python - 将三个列表合并为 1 个列表
- scala - 在 Scala 中使用 SinkFunction 创建我自己的接收器
- android - 带有 FTS4 的 Android 房间实体不会创建带有“rowid”列的表
- jenkins - 如何在 Jenkins 中限制整个管道
- ios - 如何使用 Azure DevOps 增加 xcode 项目的内部版本号
- ruby-on-rails - Facebook omniauth redirect_to user_steps_path