正则表达式表示介于5和7之间的数字,它也接受空格

regex for digits between 5 and 7 which accepts spaces as well

本文关键字:数字 空格 之间 表示 正则表达式      更新时间:2023-09-26

我正在尝试实现一个正则表达式,其中只接受带空格的数字,并且数字的数目应该在5到7之间。下面是我尝试的正则表达式。

^[0-9]{5,7}''s$

但有了空间,它就不起作用了。

下面是测试字符串

12 34 56

试试这个:

^('d *){4,6}'d$

^                  start of string
 ('d *){4,6}       4 to 6 digits with 0 to n trailing spaces
            'd     final digit
              $    end of string

编辑:要在字符串的开头和结尾也允许使用空格,可以使用以下正则表达式:

^( *'d *){5,7}$

选择5到7个组,由一个包含0到n个前导空格和尾随空格的数字组成。