#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    29
    Rep Power
    0

    RegularExpression strange behavior


    Code:
    $string="SELECT * WHERE age=10 AND something='aaa' OR somethingelse='bbb'";
    preg_match_all("/where|and|or\s+(.+?)\s+and|or?.*$/i", $string, $res);
    
    print_r($res);
    I want to get an array with following:
    age=10
    something='aaa'
    somethingelse='bbb'

    ...and what im getting is:
    Code:
    Array ( [0] => Array ( [0] => WHERE [1] => AND [2] => omething='aaa' OR somethingelse='bbb' ) [1] => Array ( [0] => [1] => [2] => ) )
    What am I doing wrong?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0
    Does "/\s(\w+\d*='*[\w\d]+'*)\s?/" work?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    29
    Rep Power
    0
    Originally Posted by freewebhost
    Does "/\s(\w+\d*='*[\w\d]+'*)\s?/" work?
    Nope, it will do the work in this example but it will also work for this:
    WRE something='something'

    And it wont work for age>10, age<10 etc...

    I need to get everything that follows the "WHERE", "AND" and "OR".

    Thanks anyway.

    EDIT:

    Ok, I figured it out:
    Code:
    $string="SELECT * FROM sometable WHERE age=10 AND something='aaa' OR somethingelse='bbb'";
    preg_match_all("/[WHERE|OR|AND]\s+([\w\d_<>=]+[\w\d_'\"]+)\s?/", $string, $res);
    This does what I expected.
    Last edited by RuleMaker; August 3rd, 2009 at 11:55 AM. Reason: figured out the solution
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by RuleMaker
    ...
    Code:
    $string="SELECT * FROM sometable WHERE age=10 AND something='aaa' OR somethingelse='bbb'";
    preg_match_all("/[WHERE|OR|AND]\s+([\w\d_<>=]+[\w\d_'\"]+)\s?/", $string, $res);
    This does what I expected.
    Maybe so, but I highly doubt that this does what you expect:

    Code:
    [WHERE|OR|AND]
    The above will only match one of the following characters: 'W', 'H', 'E', 'R', '|', 'O', 'A', 'N' or 'D'.
    You probably want:

    Code:
    (WHERE|OR|AND)
    which will match "WHERE", "OR" or "AND".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    29
    Rep Power
    0
    Oops, true.

    Thanks for correcting me.
  10. #6
  11. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by RuleMaker
    Oops, true.

    Thanks for correcting me.
    No problem. Remember that the logical OR has a very low precedence. What that means is when you do: /aaa|bbb\s+ccc|ddd/ you're really doing:

    Code:
    aaa
    OR
    bbb\s+ccc
    OR
    ddd
    and not:
    Code:
    (aaa OR bbb)
    \s+
    (ccc OR ddd)
    As to your original question, this will work and is fairly easy:

    PHP Code:
    preg_match_all("/\S+=\S+/"$string$res); 
    or if there can be spaces around the equals sign:

    PHP Code:
    preg_match_all("/\S+\s*=\s*\S+/"$string$res); 
    HTH

    Comments on this post

    • freewebhost agrees
    • ishnid agrees
    Last edited by prometheuzz; August 4th, 2009 at 04:53 AM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    29
    Rep Power
    0
    As to your original question, this will work and is fairly easy:

    PHP Code:
    preg_match_all("/\S+=\S+/"$string$res); 
    or if there can be spaces around the equals sign:

    PHP Code:
    preg_match_all("/\S+\s*=\s*\S+/"$string$res); 
    HTH
    I gave a bad example string, it should work for all kinds of WHERE clauses within a query, which means there wont always be equal sign.
    There could be >< too, even != and similar comparison signs.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0
    "/\S+\s*(=|<|>|<=|>=|<>|!=)\s*\S+/" still somewhat works
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    29
    Rep Power
    0
    Originally Posted by RuleMaker
    I need to get everything that follows the "WHERE", "AND" and "OR".
    Assuming that in MySQL statements <,>,=,!= usually appears only in WHERE clause (at least I can't think of any other case right now) we could say that's correct.

    Here's mine, with a little correction:
    Code:
    /(WHERE|OR|AND)\s+([\w\d_<>=!]+[\w\d_'"]+)\s?/i
    Guys, thanks for participating and sharing your thoughts on this topic, we could say it's completely solved in many ways

IMN logo majestic logo threadwatch logo seochat tools logo