C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-05-2009, 01:27 AM   #1
Registered User
 
AsleeKaizoku's Avatar
 
Join Date: Jan 2008
Posts: 17
How to make a specific regex for C#?

Im trying to make a regular expresion which...

1) Consist of at least one non alphanumeric character (not including white spaces)
2) At least 7 characters long


So it would match something like...

Hello!1
P@ssword

but not:

Hello1 1
Password111
AsleeKaizoku is offline   Reply With Quote
Old 04-05-2009, 02:50 AM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
Alphanumeric characters are "[a-zA-Z0-9]", so non-alphanumeric would be "[^a-zA-Z0-9]".
You don't mention any rules of the oterh characters, so I'll assume almost any can do. This is the pattern ".".
The final expression could then be ".*[^a-zA-Z0-9].*".
Note that * is greedy (look this up!) so you may need to use ".*?" instead of ".*", but I don't think it matters in this case.
As for the length of at least 7 it gets a little trickier. Can't you just check the length in code?
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 04-05-2009, 03:10 AM   #3
Registered User
 
AsleeKaizoku's Avatar
 
Join Date: Jan 2008
Posts: 17
Quote:
Originally Posted by Magos View Post
Can't you just check the length in code?
Ahhh! Bloddy hell, why didn't I think of that? That makes it so much easier

Code:
Regex regexPassword = new Regex(@".*[^a-zA-Z0-9].*");
if (!regexPassword.IsMatch(txtPass.Text.ToString().Trim()))
{
  lblError.Text = "  * Must have at least 1 non-alphanumeric character";
  lblError.Visible = true;
}
else if (regexPassword.IsMatch(txtPass.Text.ToString().Trim()))
{
  string temp = txtPass.Text.ToString().Trim();
  if (temp.Length < 7)
  {
    lblError.Text = "&nbsp;&nbsp;* Password length minimum: 7.";
    lblError.Visible = true;
  }
}
Thanks a bunch
AsleeKaizoku is offline   Reply With Quote
Old 04-05-2009, 03:16 AM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
A few notes:

1) your "else if" is unneccessary, just an "else" will do (you perform that check already in the "if")
2) No need to call ToString() on TxtPass.Text, it is already a string
__________________
MagosX.com

Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Magos is offline   Reply With Quote
Old 04-05-2009, 04:08 AM   #5
Registered User
 
AsleeKaizoku's Avatar
 
Join Date: Jan 2008
Posts: 17
Quote:
Originally Posted by Magos View Post
A few notes:

1) your "else if" is unneccessary, just an "else" will do (you perform that check already in the "if")
2) No need to call ToString() on TxtPass.Text, it is already a string
Oh yeah, (1) its a direct snippet from my code so, there are actually a bunch of other else ifs later on.

(2) I keep forgetting that :P
AsleeKaizoku is offline   Reply With Quote
Old 04-05-2009, 04:06 PM   #6
Cat
Registered User
 
Join Date: May 2003
Posts: 1,199
You also said you wanted whitespace characters to not count.

I'd do @".*[^a-zA-Z\s\d].*" for your regex. Anything that's not a letter, whitespace, or number.
__________________
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
Cat is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make some specific strings hash-mobtadi C++ Programming 2 07-23-2008 11:33 AM
how to make a windows application crvenkapa C++ Programming 3 03-26-2007 09:59 AM
make all rule duffy C Programming 9 09-11-2003 01:05 PM
Question about atheists gcn_zelda A Brief History of Cprogramming.com 160 08-11-2003 11:50 AM
'functions' in make? mart_man00 C Programming 1 06-21-2003 02:16 PM


All times are GMT -6. The time now is 04:46 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22