dongmoyu0336 2013-10-09 09:07
浏览 278
已采纳

ABAB中的正则表达式匹配编号(必须相同)

i've search many answer to match ABAB pattern that match arbitrary character sort of 1212 or 2323, have read this too but i found that the pattern to match ABAB pattern not work, it also match 4444

i try to figure out the pattern to match such subject below:

2323
4545
9898

but may not match 4444 or 5555 because that is not in ABAB pattern, i name it AAAA pattern
can someone give me clue
thanks

  • 写回答

2条回答 默认 最新

  • dongmu1996 2013-10-09 09:11
    关注

    You can use this pattern:

    (\d)(?!\1)(\d)\1\2
    
    • (\d) - capture the first digit to group $1.
    • (?!\1) - check that the second digit is not the first digit.
    • (\d) - capture the second digit to group $2.
    • \1 - match the third digit if it is the same as the first.
    • \2 - match the fourth digit if it is the same as the second.

    Working example: http://www.regex101.com/r/aV2uG1

    This is a relatively confusing regular expression, and the task can be easily solved by a few lines of code. I'm not big on PHP, but this seems to work:

    $s = '1112';
    $valid = (strlen($s) === 4) && ($s[0] === $s[2]) && ($s[1] === $s[3]) &&
             ($s[0] !== $s[1]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答