Shailendra
Shailendra

Reputation: 213

Regular expression to match alphabetic string and require at least one uppercase and one lowercase (vb.net)

I am trying to write a regular expression string match in vb.net. The condition that I am trying to implement is that the string should contain only alphabets and must contain atleast one letter of both lower and upper case. i.e AAA-fail, aaa-fail, aAaA-pass.

The regular expression that I have come up with is ^(([a-z]+[A-Z]+)+|([A-Z]+[a-z]+)+)$

Can someone suggest some better/simpler regular expression for the same?

Upvotes: 5

Views: 4052

Answers (3)

Meta-Knight
Meta-Knight

Reputation: 17875

Just for fun, I tried to tackle your problem without using regular expressions.

I have the following method which checks if a string value contains characters that correspond to specified unicode categories (uppercase, lowercase, digit...)

Private Function IsValid(ByVal value As String, _
                         ByVal ParamArray categories As UnicodeCategory()) _
                         As Boolean

    'Create a hashset with valid unicode categories
    Dim validSet = New HashSet(Of UnicodeCategory)(categories)

    'Group the string value's characters by unicode category
    Dim groupedCharacters = value.GroupBy(Function(c) Char.GetUnicodeCategory(c))

    'Get an enumerable of categories contained in the string value
    Dim actualCategories = groupedCharacters.Select(Function(group) group.Key)

    'Return true if the actual categories correspond 
    'to the array of valid categories
    Return validSet.SetEquals(actualCategories)
End Function

The method can be used this way:

Dim myString As String = "aAbbC"
Dim validString As Boolean = IsValid(myString, _
                                     UnicodeCategory.LowercaseLetter, _
                                     UnicodeCategory.UppercaseLetter)

Using this method, you can allow uppercase, lowercase AND digit characters without changing anything. Just add a third argument to IsValid: UnicodeCategory.DecimalDigitNumber

Upvotes: 1

Templar
Templar

Reputation: 5135

The regex you created will fail under some conditions, such as "aAb". I think the following will work better for you:

^(?:[a-z]+[A-Z]+|[A-Z]+[a-z]+)(?:[a-zA-Z])*$

Upvotes: 4

RaYell
RaYell

Reputation: 70444

This RegEx will work for you:

^[a-zA-Z]*([A-Z][a-z]|[a-z][A-Z])[a-zA-Z]*$

Explanation: if string must have at least one lowercase and one uppercase letter there is a point where uppercase and lowercase letters are next to each other. This place is matched by ([A-Z][a-z]|[a-z][A-Z]) and it matches both cases: one where uppercase char is first and where it's second, then if you have this criteria met you just could add an arbitrary number of lowercase of uppercase character at any end of the string and it will still match

Upvotes: 4

Related Questions