dongliao1860 2017-05-23 04:30
浏览 36
已采纳

条件正则表达式匹配基于两个非捕获组

I've been looking for what seems like hours, and I just cannot find a solution for this. My regex skills are terrible but I'm sure for someone with the knowledge this will be easy.

/^(?:[1-9]{1,3}GB)|(?:[0-9]{1,2}.[0-9]{1,2}TB)$/i

As you can see it's pretty simple. I just need to match one or the other but I don't know where I'm going wrong.

Examples:

<?= $validate->data('100GB'); ?>

This should match since it's 3 digits followed by "GB."

<?= $validate->data('2.65TB'); ?>

This should also match since it's 1 digit followed by period, followed by two more digits, followed by "TB."

EDIT: I needed to modify the numeric count but still doesn't perform as expected.

For anyone interested, here is the final regex.

/^(?:[0-9]{1,3}(?:GB|TB))|(?:[0-9]{1,2}\.[0-9]{1,2}(?:GB|TB))$/i
  • 写回答

2条回答 默认 最新

  • dsqa6272 2017-05-23 04:57
    关注

    You are not including zeros in your regex, that is why the pattern fails.

    [1-9]{3} will only match three digits 1-9. So 111 is a match. 234 is also a match.
    But 500 is not.

    With this regex: ([0-9]{3}GB)|([0-9]{1,2}.[0-9]{1,2}TB) you will be able to catch the zeros and it will not limit you to two digit TB disks.

    https://regex101.com/r/6undez/3

    EDIT: In case you want to match 100 GB note the space. You can use this regex:
    https://regex101.com/r/6undez/4
    ([0-9]{3}\s?GB)|([0-9]{1,2}.[0-9]{1,2}\s?TB)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用