Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

How to accept certain symbols and reject others

Last post 11-15-2007, 7:56 AM by RobertH. 13 replies.
Sort Posts: Previous Next
  •  11-13-2007, 11:07 AM 36459

    How to accept certain symbols and reject others

    I'm trying to create a regex that has the following requirements:

    1. must be at least 7 characters in length

    2. must contain at least 1 digit

    3. must contain at least 1 lowercase letter

    4. must contain at least 1 uppercase letter

    5. must contain at least one of the following symbols: = ! $ % ^ & +

    6. It must reject any word that contains the following symbols, as well as whitespace: " # ' ( ) , - . / : ; < > ? @ [ \ ] | * _ { } ~ `  \s

    7. And the order of the characters should not matter.

    This will be on a website with Asp.Net 2.0 using C#. I have a partial regex completed and it verifies the first 5 conditions, but if you meet the first five, it will pass any word that contains a symbol not listed in rule 5. I need it to verify rules 1-5 are valid and fail any word that contains extra symbols not listed in rule 5.

    Test cases pass:

    Abcdef1=
    ABCDf1!$
    ABCDf1$
    ABCDf1%
    ABCDf1^
    ABCDfc14&&
    ABCDf1+

    Cases should fail but pass:

    ABCDf1)^
    ABCDf1&(
    ABCDf1+@

     

    public bool ValidatePassword(string Pwd)

    {

    Regex passreg = new Regex("^.*(?=.{7,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#$%^!&+=]).*$");

    Match mRes = passreg.Match(Pwd);

    return mRes.Success;

    }

    Thanks in advance!

    Robert

  •  11-13-2007, 4:28 PM 36479 in reply to 36459

    Re: How to accept certain symbols and reject others

    Try

    ^(?!.*[-\x22\x23\x27(),./:;<>?@\[\\\]|*_{}~`\s])(?=.{7,}$)(?=.*\p{Ll})(?=.*\p{Lu})(?=.*\d)(?=.*[=!$%^&+]).+$ 

     

    You'll need to escape things for C#. Also in case this if for a browser the is bug in IE that will cause this to fail. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-14-2007, 7:16 AM 36511 in reply to 36479

    Re: How to accept certain symbols and reject others

    Thanks! And yea, it'll be via browser. ASP.Net 2.0 front end with C# code behind - server side.

     So even though it's server side code, IE will still cause it to fail? :-(

  •  11-14-2007, 7:42 AM 36512 in reply to 36511

    Re: How to accept certain symbols and reject others

    I tried it and I'm getting "Unrecognized escape sequence" errors on the blue & bolded characters below: BTW

    ^(?!.*[-\x22\x23\x27(),./:;<>?@\[\\\]|*_{}~`\s])(?=.{7,}$)(?=.*\p{Ll})(?=.*\p{Lu})(?=.*\d)(?=.*[=!$%^&+]).+$

    BTW - Why is it that ASP.Net/C# is accepting (?=.*\\d) instead of the correct (?=.*\d) format? By looking at it, it looks like the \ is being escaped and viewed as a \ or d statement instead of the intended [0-9] block... But when run, (?=.*\\d) does actually represent [0-9] while (?=.*\d) just errors out... I do remember reading something about needing to use multiple \'s to escape the \ properly, but I thought that was for... c++(?) and not .Net?

     

  •  11-14-2007, 10:46 AM 36516 in reply to 36512

    Re: How to accept certain symbols and reject others

    C# uses the C++ syntax, so there are two layers of escape characters.  First you have C#'s syntax, so \ becomes \\.  Then you have Regex's escape sequence.

    So, for example, to look for the literal \ via Regex in C#, you have to look for \\\\

  •  11-14-2007, 11:10 AM 36521 in reply to 36512

    Re: How to accept certain symbols and reject others

    RobertH:

    I tried it and I'm getting "Unrecognized escape sequence" errors on the blue & bolded characters below: BTW

    ^(?!.*[-\x22\x23\x27(),./:;<>?@\[\\\]|*_{}~`\s])(?=.{7,}$)(?=.*\p{Ll})(?=.*\p{Lu})(?=.*\d)(?=.*[=!$%^&+]).+$

    BTW - Why is it that ASP.Net/C# is accepting (?=.*\\d) instead of the correct (?=.*\d) format? By looking at it, it looks like the \ is being escaped and viewed as a \ or d statement instead of the intended [0-9] block... But when run, (?=.*\\d) does actually represent [0-9] while (?=.*\d) just errors out... I do remember reading something about needing to use multiple \'s to escape the \ properly, but I thought that was for... c++(?) and not .Net?

     

    Yeah what Lyndar said.  That's what I meant when I said you'd have to escape it for C#. If you want to use it as written you can prefix your C# string with the 'at' sign.

    String ex1 = @"\d"
    String ex2 = "\\d"

    evaluate to the same thing in C# 

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-14-2007, 11:14 AM 36522 in reply to 36516

    Re: How to accept certain symbols and reject others

    I see... So then basically to properly escape the characters listed above (in blue) in the regex earlier, I would need to do \\[ \\] \\\\ \\s and \\p to fix the unrecognized escape sequence errors. for the [ ] and \ characters, do you think would it just be easier using the \x5B, ... hex values instead of all the \'s. Actually I can't decide which is easier to decypher, a bunch of \'s or hex code...

    I can already see regex is going to be fun. But at least I got an interesting one for my first shot at working with them :-)

  •  11-14-2007, 12:15 PM 36528 in reply to 36512

    Re: How to accept certain symbols and reject others

    RobertH:

    I tried it and I'm getting "Unrecognized escape sequence" errors on the blue & bolded characters below: BTW

    ^(?!.*[-\x22\x23\x27(),./:;<>?@\[\\\]|*_{}~`\s])(?=.{7,}$)(?=.*\p{Ll})(?=.*\p{Lu})(?=.*\d)(?=.*[=!$%^&+]).+$

    Ok, so I properly escaped the s, p and d characters and just used hex for the - [ ] \ and {} characters but it is still allowing rejected characters (like space, @ and the others too), any ideas?

    Regex passreg = new Regex("^(?!.*[\x2D\x22\x23\x27\x5C\x7C\x5D\x5B\x7D\x7B(),./:;<>?@|*_~`\\s])(?=.{7,}$)(?=.*\\p{Ll})(?=.*\\p{Lu})(?=.*\\d)(?=.*[=!$%^&+]).+$");

    // Hex conversions: \x5C = \ \x7C = | \x5B = [ \x5D = ] \x2D = - \x7B = { \x7D = } \x22 = " \x23 = # \x27 = '
    Match mRes = passreg.Match(Pwd);
    return mRes.Success;

  •  11-14-2007, 12:30 PM 36529 in reply to 36522

    Re: How to accept certain symbols and reject others

    RobertH:

    I see... So then basically to properly escape the characters listed above (in blue) in the regex earlier, I would need to do \\[ \\] \\\\ \\s and \\p to fix the unrecognized escape sequence errors. for the [ ] and \ characters, do you think would it just be easier using the \x5B, ... hex values instead of all the \'s. Actually I can't decide which is easier to decypher, a bunch of \'s or hex code...

    I can already see regex is going to be fun. But at least I got an interesting one for my first shot at working with them :-)

    It would be easier to use the at character as I mentioned.

    String regexpattern = @"^(?!.*[-\x22\x23\x27(),./:;<>?@\[\\\]|*_{}~`\s])(?=.{7,}$)(?=.*\p{Ll})(?=.*\p{Lu})(?=.*\d)(?=.*[=!$%^&+]).+$ ";


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-14-2007, 12:40 PM 36530 in reply to 36529

    Re: How to accept certain symbols and reject others

    Yes, I do agree. I tried playing with it as hex and it slowed me down a lot having to keep looking back and trying to convert it.

    Ok, so this is what looks like is happening, I tried pulling all the rejects out and just added it back in 1 at a time and tested after each add. It looks like once I add the [ ] back in, that's when it starts ignoring the other invalid characters. could it be trying to end the regex too soon even though it's been entered as a literal and not a special character? If i remove the [] from the list and leave everything else in there, it finds the rejected characters without issues, then stops when the [] is added back. to be more precise, it was fine with just the [ added, but crapped out after ] was added. I'm guessing, that just like the - symbol, that the ] might have to come at a special spot in the regex?

  •  11-14-2007, 12:49 PM 36531 in reply to 36530

    Re: How to accept certain symbols and reject others

    Oh, and in case I haven't mentioned it lately, THANK YOU SO MUCH FOR YOUR HELP! I'm learning a lot from this experience :-) I'm probably going to know more than I ever thought I wanted to know after this is done. ;-)

  •  11-14-2007, 2:40 PM 36536 in reply to 36531

    Re: How to accept certain symbols and reject others

    A variation:

    ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[=!\$%\^&\+])[^"#'\(\),\./:;<>\?@\[\\\]\|\*_{}~'\s-]{7,}$

    I didn't use Hex, so you may need to replace a couple characters with their hex equivalent for compatibility with C#.

  •  11-15-2007, 1:02 AM 36546 in reply to 36530

    Re: How to accept certain symbols and reject others

    OK I don't what you are doing but the original regex I sent you works against the sample data provided

    How are you testing this? Serverside or Clientside in the browser.  Can you post your current code snippet that is building and executing the regex 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-15-2007, 7:56 AM 36561 in reply to 36546

    Re: How to accept certain symbols and reject others

    mash:

    OK I don't what you are doing but the original regex I sent you works against the sample data provided

    How are you testing this? Serverside or Clientside in the browser.  Can you post your current code snippet that is building and executing the regex 

     Hmm... Ok, I don't know what to say... I spent a big part of yesterday testing it with and without the ] and yesterday it kept ignoring any of the rejected symbols when the ] was placed in, but worked fine when I removed it... I put it back in this morning after reading your post, so that I can post the code on here, and went ahead and tested it again, and this time it rejected all the symbols like it was supposed to... Nothing different from yesterday to today (that I'm aware of)... I guess either I was doing something wrong and didn't realize it (which would be weird since I just copy and pasted your exact regex string using the @ in front), or the website was having a bad day and didn't want to cooperate... It's a mystery to me, but as long as it keeps working I'll be happy.

     Thanks again for yall's help. I know this regex is probably a simple one, but I just couldn't figure out how to put both accepted and rejected symbols into the same regex line. Not to mention, some of the escaped characters were giving me a headache too hehe. But now I know and I'm learning :-D 

View as RSS news feed in XML