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

    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Regex on a querystring


    Hello,
    I'm trying to parse a search querystring.
    Here's an example querystring.
    Code:
    ?k=ALL(test1)%20%22test2%22%20ANY(test3)%20NONE(test4)%20(Author%3Atest5%20AND%20Description%3Atest6)
    When I get the value for 'k' in the querystring it comes out as
    Code:
    ALL(test1) "test2" ANY(test3) NONE(test4) (Author:test5 AND Description=test6)
    I need to split this string into its constituent parts depending on the space characters but respecting the brackets:

    So, Id get it matched as...

    ALL(test1)
    "test2"
    ANY(test3)
    NONE(test4)
    (Author:test5 AND Description=test6)

    It's the last line with the spaces inside the brackets that is causing me problems.
    Thanks for any help.
    Last edited by biomorph; November 2nd, 2011 at 06:46 AM. Reason: spelling mistake
  2. #2
  3. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93

    working regex


    With a preg_match_all, this expression will return all the strings you specified.

    Code:
    (?:ALL\([^)]*?\))|(?:%22(?:(?!%22).)*?%22)|(?:ANY\([^)]*?\))|(?:NONE\([^)]*?\))|(?:ANY\([^)]*?\))|\([^)]*?\)
    The matches:

    ALL(test1)
    %22test2%22
    ANY(test3)
    NONE(test4)
    (Author%3Atest5%20AND%20Description%3Atest6 )

    Is this what you are looking for?

IMN logo majestic logo threadwatch logo seochat tools logo