VB.Net 2.0 Regular Expressions

koshy

New member
Joined
Feb 11, 2007
Messages
3
Location
Dubai,United Arab Emirates
Programming Experience
1-3
Any one please help me with the Regular Expressions which allow Alphabets only, i tried this [^a-zA-Z] but it's allowing Numeric also.

Case INPUTTYPES_SETTINGS.AlphabetsOnly
_RegularExp = New System.Text.RegularExpressions.Regex("[^a-zA-Z]")
If (_RegularExp.IsMatch(_TxtBox.Text) = False) Then
Call PopupBalloon("Alphabets Expected..!!", ToolTipIcon.Error)
End If
Case KTB_INPUTTYPES_SETTINGS.AlphaNumeric

 
koshy,

Remove the "^" character.

This is the "NOT" character in Regular Expressions. So what you are saying is to find all characters that are NOT in your list.

Good Luck,
Harold
 
You have created a negated character class, ie matching a single char NOT among the listed.

So you must remove the character class negating sign ^. The result is a match if any single char among the listed is found. For example "123x456" will match because it finds the 'x'.

So you need repetition, unless you want to match only one character in Textbox which would be very unusual (and defeat the purpose and also not what you asked for actually), for example '+' sign to repeat the character class one or more times and is greedy, but this will also match the string example above because 'x' is found within the given range one (or more) time. Also "123ax456" will match.

What you have to do is then limit the expression by the start of string sign "^" and the end of string sign "$".

This gives the full expression in overview:
1) start of string
2) character range [a-zA-Z], any single characters in list
3) greedy repetition one or more, +
4) end of string
VB.NET:
^[a-zA-Z]+$

Recommended reading: http://www.regular-expressions.info
 
Back
Top