Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

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?

like image 487
Shailendra Avatar asked Jul 20 '09 18:07

Shailendra


3 Answers

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])*$
like image 52
Templar Avatar answered Sep 26 '22 14:09

Templar


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

like image 22
RaYell Avatar answered Sep 26 '22 14:09

RaYell


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

like image 31
Meta-Knight Avatar answered Sep 23 '22 14:09

Meta-Knight