regex - VB.NET & simple Regular Expression -
i've got extremely simple regular expression contained in subroutine:
private sub txtsubject_leave() handles txtsubject.leave dim pattern string = "^[a-z0-9]*" if not regex.ismatch(txtsubject.text, pattern) msgbox("invalid subject") txtsubject.focus() end if end sub
i'm sure it's going simple every string try erroneous wont return error message, doing wrong?
^[a-z0-9]*
matches 0 or more alphanumeric characters @ start of string trying match. 0
part important here, because if first character of string space, 0 'match' , pass test. if want make sure first character alphanumeric, use quantifier, +
:
^[a-z0-9]+
if want make sure string contains alphanumeric characters, use end of line anchor well:
^[a-z0-9]+$
in particular situation, if want allow empty string, can use *
:
^[a-z0-9]*$
this 1 not allow non-alphanumeric characters either.
Comments
Post a Comment