Thread: Regex question

Page 2 of 2 First 12
  • Jump to page:
    #16
  1. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by paulh1983
    what is wrong with one liner regex as opposed to 10 lines?
    Good luck writing that one-liner.

    No, it's not just a long character class, because that only checks whether any of the characters is present. So '/[a-zA-Z0-9]{10,}/' would happily accept 'aaaaaaaaaa', even if there's no uppercase letter and no digit at all. And it's not just a sequence of character classes, because you have to account for all possible combinations: First a lowercase letter, then an uppercase letter, then a digit. First a lowercase letter, then a digit, then an uppercase letter etc. That'll be a very long "one-liner".

    Forget about the single regex. What you can do, however, is avoid the repitions by using a loop:

    PHP Code:
    <?php

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

    $valid true;
    foreach (
    $required as $pattern)
        if (!
    preg_match($pattern$incoming))
            
    $valid false;
    Note that the dot has no special meaning inside a character class. But the hypen does and must be put at the beginning or the end. A functional backslash in a regex is a double backslash in the string. And a literal backslash in a regex is a quadruple backslash in the string.
    Last edited by Jacques1; September 13th, 2013 at 06:04 AM.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  2. #17
  3. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    960
    Rep Power
    97
    Originally Posted by paulh1983
    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
    The code is not wrong. It is simple. I prefer writing simpler code because I may forget how it works months from now when I need to update it or if someone else comes in after me to modify it.

    This is a function. I don't need to post all of my code here to get the help I'm asking for. You would know that if you read the entire thread.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

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

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    960
    Rep Power
    97
    Originally Posted by Jacques1
    Forget about the single regex. What you can do, however, is avoid the repitions by using a loop:

    PHP Code:
    <?php

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

    $valid true;
    foreach (
    $required as $pattern)
        if (!
    preg_match($pattern$incoming))
            
    $valid false;
    Note that the dot has no special meaning inside a character class. But the hypen does and must be put at the beginning or the end. A functional backslash in a regex is a double backslash in the string. And a literal backslash in a regex is a quadruple backslash in the string.
    That is a bit of coolness. Never thought of putting these into an array.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo