Regular expressions: exclude a character from a set.

Is there a way to exclude a specific character from a set like [A-Z] or [0-9] For example, suppose I want to exclude 'D' from [A-Z], I could write

[A-CE-Z]. But that syntax doesn't suit my scenario very well. Isn't there any other syntax for this



Answer this question

Regular expressions: exclude a character from a set.

  • Steve Eichert

    Hello Becko,
    In order to match A-Z excluding 'D' the correct expression is [A-CE-Z]. Likewise to match 0-9 excluding '5' the expression is [0-46-9].

    If the [A-CE-Z] syntax does not suit your scenario very well, you could use two regular expressions instead. First match [A-Z] and then check the match.Value for the presence of "D" (with Drinks or the absence of "D" with [^D]).

    Here is a code sample that tests several strings for "([A-Z][0-9])+" and then checks the matches against "([D5])+" to exclude strings with one or more "D" or "5" values:

  • Regular expressions: exclude a character from a set.