Exclude certain numbers from range of numbers using Regular expression

試著忘記壹切 提交于 2020-01-03 12:59:20

问题


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

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.


回答1:


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

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

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

  • /^((?!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
        • 2nd Alternative: 504
          • 504 matches the characters 504 literally
      • [0-9]* match a single character present in the list below
        • Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        • 0-9 a single character in the range between 0 and 9
    • $ assert position at end of a line
    • m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
    • g modifier: global. All matches (don't return on first match)


  • 来源:https://stackoverflow.com/questions/35592438/exclude-certain-numbers-from-range-of-numbers-using-regular-expression

    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!