By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

RegEx Help

P: n/a
slg
Gurus,
I am new to RegEx. How can i validate following.
All characters in my strings are [A-Za-z0-9] and underscore

The string MUST begin with Upper Or lowercase character.
Maximum length is 51
can have underscores in between but no spaces any where
cannot end with underscore
Valid
=======
John_Smith
John
JOHN
J129
J129_89
J129_89_aBC

Invalid
=========
J__33 == has two underscores continuously
J__33_ == ends with underscore

TIA

Nov 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gurus, I am new to RegEx. How can i validate following. All characters
in my strings are [A-Za-z0-9] and underscore

The string MUST begin with Upper Or lowercase character.
Maximum length is 51
can have underscores in between but no spaces any where
cannot end with underscore
Valid
=======
John_Smith
John
JOHN
J129
J129_89
J129_89_aBC
Invalid
=========
J__33 == has two underscores continuously
J__33_ == ends with underscore
TIA
How about:
^[A-Za-z][A-Za-z0-9]*(_[A-Za-z0-9]+)*$

That should take care of everything but the length requirement. You may be
able to get that working as well, using something like

^[A-Za-z]([A-Za-z0-9]|(_(?=[A-Za-z0-9])){0,50}$

but that is a bit more tricky.

Note: I haven't tested these!
Nov 6 '07 #2

P: n/a
Hello slg,

Not that hard is it?

[A-Za-z](_?[A-Za-z0-9])*

guessing that a one character sting is Ok as well. if not, make the * at
the end a +.

Checking for a strings length is something that String.Lentgh was designed
for. Or where you can use the MaxLength of a textbox.

Jesse
Gurus, I am new to RegEx. How can i validate following. All characters
in my strings are [A-Za-z0-9] and underscore

The string MUST begin with Upper Or lowercase character.
Maximum length is 51
can have underscores in between but no spaces any where
cannot end with underscore
Valid
=======
John_Smith
John
JOHN
J129
J129_89
J129_89_aBC
Invalid
=========
J__33 == has two underscores continuously
J__33_ == ends with underscore
TIA
--
Jesse Houwing
jesse.houwing at sogeti.nl
Nov 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.