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 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥20 关于URL获取的参数,无法执行二选一查询