Dan
Dan

Reputation: 4663

VB.NET Regex Boolean Match

I am score a password based on various features, but I don't believe my RegEx is correct:

    If Regex.IsMatch(password, "/\d+/", RegexOptions.ECMAScript) Then
        'Contains a number
        score += 1
    End If

    If Regex.IsMatch(password, "/[a-z]/", RegexOptions.ECMAScript) Then
        'Contains a lowercase letter
        score += 1
    End If

    If Regex.IsMatch(password, "/[A-Z]/", RegexOptions.ECMAScript) Then
        'Contains an uppercase letter
        score += 1
    End If

    If Regex.IsMatch(password, "/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript) Then
        'Contains special character
        score += 2
    End If

How do I fix this? I believe these are formatted for C# not VB.NET.

Upvotes: 0

Views: 779

Answers (1)

SLaks
SLaks

Reputation: 888107

The .Net Regex class takes the raw text of the regular expression.

You should not wrap it in / characters; those simply match the literal text /.

Some other notes:

  • You don't need RegexOptions.ECMAScript
  • Character classes are not comma-separated
  • You're missing a large number of special characters. Use a negated class (all non-alphanumeric chars)
  • You can make them faster by pre-compiling them into reusable Regex instances instead of re-parsing each regex every time.

Upvotes: 1

Related Questions