Home > front end >  Regex for "everything except 34,37" WITHOUT negative lookahead
Regex for "everything except 34,37" WITHOUT negative lookahead

Time:01-12

Every time this is asked, the answer is to use negative lookahead like so: (?!x)

Well unfortunately I must implement an American Express check in vb6, which does NOT support negative lookaheads or negative look behinds.

Is it possible to match everything except 34, 37 using the oldest imaginable implementation of regular expressions?

I expected this sort of thing to work: ^[1,2,4-9][1-3,5-6,8-9][0-9]{13}$

I've been able to match AmEx with this expression of course: ^3[47][0-9]{13}$ but I haven't found a way to inverse the validation rule.

Here is my validator setup:

       <asp:RegularExpressionValidator
            ID="AmExValidator"
            Display="None"
            ControlToValidate="txtCreditCardNumber"
            ValidationExpression="^[1,2,4-9][1-3,5-6,8-9][0-9]{13}$"
            ErrorMessage="American Express cards are not accepted."
            ValidationGroup="Payment"
            runat="server">
        </asp:RegularExpressionValidator>```

CodePudding user response:

You can use

^([0-24-9][0-9]|3[0-35689])[0-9]{13}$

See the regex demo. Details:

  • ^ - start of string
  • ([0-24-9][0-9]|3[0-35689]) - 00 to 29 and 40 to 99, or 3 followed with a digit other than 4 and 7
  • [0-9]{13} - thirteen digits
  • $ - end of string.
  •  Tags:  
  • Related