使用正则表达式从数字范围中排除某些数字 [英] Exclude certain numbers from range of numbers using Regular expression

查看:922
本文介绍了使用正则表达式从数字范围中排除某些数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我使用正则表达式吗?在正则表达式中,我们可以从一系列数字中排除某些介于中间的数字.

Could some one help me with the regular expression, where we can exclude certain numbers in between from a range of numbers.

当前,^([1-9][0][0-9])$是已配置的正则表达式.现在,如果我想从中排除一些数字/一个数字(501504),那么正则表达式的外观如何.

Presently, ^([1-9][0][0-9])$ is the regular expression that is configured. Now if i want to exclude a few numbers/one number(501,504) from it, then how would the regular expression look/be.

推荐答案

在此答案中更详细地描述,您可以将以下正则表达式与负向超前"命令?!一起使用:

Described in more detail in this answer, you can use the following regex with the "Negative Lookahead" command ?!:

^((?!501|504)[0-9]*)$

您可以看到正在执行的正则表达式&此处说明: https://regex101.com/r/mL0eG4/1

You can see the regex being executed & explained here: https://regex101.com/r/mL0eG4/1

  • /^((?! 501 | 504)[ 0-9 ] *)$/mg
    • ^在行的开头断言位置
    • 第一个捕获组((?!501 | 504)[0-9] *)
      • (?! 501 | 504)负前瞻-断言不可能匹配下面的正则表达式
        • 第一种选择:501
          • 501从字面上匹配字符501
  • /^((?!501|504)[0-9]*)$/mg
    • ^ assert position at start of a line
    • 1st Capturing group ((?!501|504)[0-9]*)
      • (?!501|504) Negative Lookahead - Assert that it is impossible to match the regex below
        • 1st Alternative: 501
          • 501 matches the characters 501 literally
    • 504从字面上匹配字符504
    • 量词:*零次至无限次,尽可能多地返回[greedy]
    • 0-9所需的单个字符,范围在0到9之间

    这篇关于使用正则表达式从数字范围中排除某些数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆