Search code examples
regexposix

Regex for detecting a query string


I'm trying to use regex for testing a URL.

  • accepted cases:

    www.mystite.com/whatever/?bla=dfgsd&page=test 
    www.mystite.com/whatever/?bla=dfgsd&page=test&
    www.mystite.com/whatever/?bla=dfgsd&page=test&hello=bla 
    www.mystite.com/whatever/?page=test&hello=bla
    
  • rejected:

    www.mystite.com/whatever/?hello=bla&page=testfff 
    www.mystite.com/whatever/?page=testfff&hello=bla - NOT good
    

because misses &page=test&, page=test& or &page=test

That's what I tried, and it doesn't work:

([^&]+)([page=test])([^&]+)

You can ignore the domain and sub folder, what I'm trying to check is only after the question mark


Solution

  • After reading the question for the 3rd time I think I now get what you are asking for:

    &?page=test($|&)
    

    This matches all occurences of "page=test" with optional "&" at the start. At the end there needs to be either an "&" (optionally followed by other characters) or nothing (end of string).

    Does this solve your problem?