dtlc84438 2017-06-11 13:06
浏览 373
已采纳

带有或不带小数的百分比的正则表达式

Im trying to use a regular expression to validate strings like:

1 %
12,25 %
99,99 %
100 %
100,00 %

Tried with this one from here

^(100\,00|[1-9]?\d\,\d{2}) %$

but i need the decimals to be optional and this one fails with strings like "90 %"

Thank you in advance,

  • 写回答

2条回答 默认 最新

  • dtcrw26206 2017-06-11 13:13
    关注
    /^(10{2}(?:,0{2})?|[1-9]?\d(?:,\d{2})?) %$/
    

    The pattern allows a percentage of 100,00 or a 1 or 2 digit number (2-digit not starting with 0) optionally followed by a comma and 2 digits.

    See Pattern Demo for matching, explanation, and efficiency.

    Bonus features:

    My pattern uses quantifiers on repeated characters to improve performance.
    It doesn't use escaping slashes on commas -- they're not necessary.
    It doesn't use more than one capture group, following the OP's pattern design.

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

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了