Regex - Exclude "-" Between Numbers

Exclude certain numbers from range of numbers using Regular expression

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)
  • Regex to exclude a range of numbers

    Thank you for the answers (which all work correctly).

    Bohemian was also correct in saying that:

    it ends with \.* but should end with \..* or more precisely \.\d*

    So I edited my regular expression to the following which now works fine:

    ([0-9][0-9]\.\d*)|(1[0-9][0-9]\.\d*)

    A regular expression to exclude a word/string

    Here's yet another way (using a negative look-ahead):

    ^/(?!ignoreme|ignoreme2|ignoremeN)([a-z0-9]+)$ 

    Note: There's only one capturing expression: ([a-z0-9]+).

    Exclude a combination of characters with regex or add a letter

    You may either find \b([0-9]{2})\b regex matches and replace with E$1, or match \bs(0[1-9]|[1-9][0-9])\b pattern in an ignore filter.

    Details

    • \b([0-9]{2})\b - matches and captures into Group 1 any two digits that are not enclosed with letters, digits and _. The E$1 replacement means that the matched text (two digits) is replaced with itself (since $1 refers to the Group 1 value) with E prepended to the value.
    • \bs(0[1-9]|[1-9][0-9])\b - matches an s followed with number between 01 and 99 because (0[1-9]|[1-9][0-9]) is a capturing group matching either 0 and then any digit from 1 to 9 ([1-9]), or (|) any digit from 1 to 9 ([1-9]) and then any digit ([0-9]).

    NOTE: If you need to generate a number range regex, you may use this JSFiddle of mine.

    What's a regex that matches all numbers except 1, 2 and 25?

    Not that a regex is the best tool for this, but if you insist...

    Use a negative lookahead:

    /^(?!(?:1|2|25)$)\d+/

    See it here in action: http://regexr.com/39df2

    Regex excluding specific characters

    For that specific lesson, the correct regex is:

    [^b]og

    EXPLANATION:

    /[^b]og/

    [^b] match a single character not present in the list below
    b the literal character b (case sensitive)
    og matches the characters og literally (case sensitive)

    NOTES:

    Negated Character Classes

    Typing a caret after the opening square bracket negates the character
    class
    . The result is that the character class matches any character
    that is not in the character class. Unlike the dot, negated character
    classes also match (invisible) line break characters. If you don't
    want a negated character class to match line breaks, you need to
    include the line break characters in the class. [^0-9\r\n] matches any
    character that is not a digit or a line break.

    It is important to remember that a negated character class still must
    match a character. q[^u] does not mean: "a q not followed by a u". It
    means: "a q followed by a character that is not a u". It does not
    match the q in the string Iraq. It does match the q and the space
    after the q in Iraq is a country. Indeed: the space becomes part of
    the overall match, because it is the "character that is not a u" that
    is matched by the negated character class in the above regexp. If you
    want the regex to match the q, and only the q, in both strings, you
    need to use negative lookahead.

    How to fix this regexp to exclude negative numbers

    Do you mean something like this? '*' means any number of matches so 0 or more, while '+' means 1 or more.

    ^\d+(?:\.\d+)?$

    Excluding some character from a range - javascript regular expression

    To exclude k or p from [a-zA-Z] you need to use a negative lookahead assertion.

    (?![kpKP])[a-zA-Z]+

    Use anchors if necessary.

    ^(?:(?![kpKP])[a-zA-Z])+$

    It checks for not of k or p before matching each character.

    OR

    ^(?!.*[kpKP])[a-zA-Z]+$

    It just excludes the lines which contains k or p and matches only those lines which contains only alphabets other than k or p.

    DEMO



    Related Topics



    Leave a reply



    Submit