Thread: How to make a specific regex for C#?

  1. #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

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #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

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  5. #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

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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