Thread: Regex question

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96

    Regex question


    Can someone tell me what difference is between these:

    PHP Code:
    if (!preg_match('/[A-Z][a-z]/',$incoming)) { 
    and

    PHP Code:
    if (!preg_match('/[A-Za-z]/',$incoming)) { 


    My ultimate goal is to check the $incoming string to ensure it has BOTH upper and lower case letters.

    Thank you!
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    87
    Rep Power
    10
    I think the first one will display an error.

    But why don't you use

    PHP Code:
    ctype_alpha($incoming
    ?

    Does the same thing
  4. #3
  5. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96
    Testing it, the second one seems to work. The first one didn't. I'll test it more aggressively to be sure.

    ctype_alpha would work, except I'm also checking for numbers and other things. I don't want one long, insane REGEX for checking a string so I'm trying to use different REGEX for checking different things.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  6. #4
  7. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96
    Confirmed.

    This fails:

    PHP Code:
    $incoming="T1gerL1ly&me";
    //has upper/lower case?
    if (!preg_match('/[A-Z][a-z]/',$incoming)) {
        echo 
    "not upper/lower case";
        echo 
    "<br>";

    This does not:

    PHP Code:
    $incoming="T1gerL1ly&me";
    //has upper/lower case?
    if (!preg_match('/[A-Za-z]/',$incoming)) {
        echo 
    "not upper/lower case";
        echo 
    "<br>";

    So back to my original question: what's the difference?
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    87
    Rep Power
    10
    And other things? Like special characters?

    If that is the case

    use this algorithm

    PHP Code:
    [^A-Za-z0-9'\.&@:?!()$#^]" 
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    87
    Rep Power
    10
    Originally Posted by Frank Grimes
    Confirmed.

    This fails:

    PHP Code:
    $incoming="T1gerL1ly&me";
    //has upper/lower case?
    if (!preg_match('/[A-Z][a-z]/',$incoming)) {
        echo 
    "not upper/lower case";
        echo 
    "<br>";

    This does not:

    PHP Code:
    $incoming="T1gerL1ly&me";
    //has upper/lower case?
    if (!preg_match('/[A-Za-z]/',$incoming)) {
        echo 
    "not upper/lower case";
        echo 
    "<br>";

    So back to my original question: what's the difference?
    I don't know what the difference, but as you said, the first in your OP doesn't work, it maybe use to check after a set different of alpha characters [ ] [ ] as to this which may be used to do the whole string [A-Za-z] in one go
  12. #7
  13. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96
    Really confused here.

    This should fail, but does not:

    PHP Code:
    $incoming="t1gerlly&me";
    if (!
    preg_match('/[^A-Za-z0-9'\.&@:?!()$#^]/,$incoming)) {
        
    echo "fail";

    To be clear, I REQUIRE all of these:

    - upper case
    - lower case
    - number
    - symbol
    - at least 10 chars

    My rudimentary knowledge of REGEX tells me this one finds those characters acceptable, not required. Am I correct?


    I've been following the examples on this page:

    http://webcheatsheet.com/php/regular_expressions.php
    Last edited by Frank Grimes; September 12th, 2013 at 11:25 AM.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  14. #8
  15. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96
    Splitting each rule of mine into its own regex seems to work fine:

    PHP Code:
    function checkPassword($incoming) {
            
    $rules=0;
            
            
    // right length?
            
    if (strlen($incoming)<10) {
                
    $rules=1;
            }
            
            
    //has upper case?
            
    if (!preg_match('/[A-Z]/',$incoming)) {
                
    $rules=1;
            }
            
            
    //has lower case?
            
    if (!preg_match('/[a-z]/',$incoming)) {
                
    $rules=1;
            }
                        
            
    //has a nubmer?
            
    if (!preg_match('/[0-9]/',$incoming)) {
                
    $rules=1;
            }
                        
            
    //has symbol?
            
    if (!preg_match('/[`~|!@#$%^&*()\-_=+{};:,<.>]/',$incoming)) {
                
    $rules=1;
            }
                        
            if (
    $rules==0) {
                return 
    TRUE;
            } else {
                return 
    FALSE;
            }
        } 
    just wondering why I couldn't combine the upper/lower case rules into one.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    the top one would not fail if you tested it with something like "Ab". the bottom passes but you are searching for exactly 1 character. regex should contain + or * something like

    '/[A-Za-z0-9]+/'

    + = one or more...

    also both examples you posted do not check for numbers which you mentioned you wanted.

    proper regex should be /'^[A-Za-z0-9]+$'/ so you are checking the string FROM start to end and if it has spaces it would fail.

    if you want to have a min/max character limit then do

    /'^[A-Za-z0-9]{1,9}$'/ = min 1 max 9 char
  18. #10
  19. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96
    Thank you for the help!

    My requirement is the string have at least 1 uppercase letter and at least 1 lowercase letter. Does adding the + as you have mean it can be in any place in the string or just have more than one?

    I don't care how many there are as long as there is at least one of each.

    Man, can't believe this is so hard.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Splitting it into separate checks is the right solution. Regular expressions are used for PATTERNS, not for RULES. [A-Za-z] means "one character, which can be either an upper or lower case english letter." It does not mean "the entire string should contain one upper case and one lower case letter at a minimum."
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96
    Originally Posted by ManiacDan
    Splitting it into separate checks is the right solution. Regular expressions are used for PATTERNS, not for RULES. [A-Za-z] means "one character, which can be either an upper or lower case english letter." It does not mean "the entire string should contain one upper case and one lower case letter at a minimum."
    That was my thinking from the start. First off, I didn't want a ridiculously long regex to troubleshoot two months from now when I couldn't remember how it worked and multiple checks seem easier to change if my requirements change in the future.

    So, to confirm, this will check for my requirements:

    - at least 10 chars
    - at least 1 uppercase
    - at least 1 lowercase
    - at least 1 symbol
    - at least 1 number

    ?

    PHP Code:
    // right length?
            
    if (strlen($incoming)<10) {
                echo 
    "bad length";
                echo 
    "<br>";
            }
            
            
    //has upper case?
            
    if (!preg_match('/[A-Z]/',$incoming)) {
                echo 
    "not upper case";
                echo 
    "<br>";
            }
            
            
    //has lower case?
            
    if (!preg_match('/[a-z]/',$incoming)) {
                echo 
    "not lower case";
                echo 
    "<br>";
            }
                        
            
    //has a nubmer?
            
    if (!preg_match('/[0-9]/',$incoming)) {
                echo 
    "no number";
                echo 
    "<br>";
            }
                        
            
    //has symbol?
            
    if (!preg_match('/[`~|!@#$%^&*()\-_=+{};:,<.>]/',$incoming)) {
                echo 
    "no symbol";
                echo 
    "<br>";
            } 
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Your last regex is better off as:
    if (!preg_match('/[`~|!@#$%^&*()\\-_=+{};:,<>\.]/',$incoming)) {

    Otherwise, this seems fine. Remember aside from echoing you also have to detect if there's a failure and actually act on that (don't create the account or whatever).
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  26. #14
  27. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    941
    Rep Power
    96
    Perfect. Thank you!

    This is only the function that checks for the requirements. It returns TRUE or FALSE and based on that hashes, saves, etc. and tells the user it's good or why it failed.

    Thanks again!
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    that is a wrong and tedious code and if i was to repeat it every time, it would be a nightmare.

    what is wrong with one liner regex as opposed to 10 lines? if you need to understand what it does write a comment line or two and you will still get it in a months time.

    I would also consider putting your if/elses into function/classes so you can reuse it again & again
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo