Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Hard/Easy Regular Expression help

Last post 08-01-2010, 7:44 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  07-31-2010, 1:38 AM 70311

    Hard/Easy Regular Expression help

    Hello Everyone,

    I need help to get regular expressions to match urls in that way.

    Used language: PHP

    First One:-

    (http:// only works with http protocol) (with www or without) (dot.) (domain name what ever it has numbers or chars) (dot.) (com|org|us etc...any domain extension is accept)(/ must have slash)(Numbers Only)(/ with ending slash or without)

    examples:-

    http://www.example.com/123123

     http://example.us/41233/

      http://www.example.info/41233/

     

    The second one which I believe its hard to done:-

    (http:// only works with http protocol) (with anything here numbers and chars www2 ww4 links etc ..) (dot.) (domain name what ever it has numbers or chars) (dot.) (com|org|us etc...any domain extension is accept)(/ must have slash)(foldername maybe having a folder name or not)(any-file-name.php?((url|link|redirect|go|anything=(what ever comes here)

    examples:-

    http://www.example.com/dir/red.php?url=http://www.example.com

    http://example.us/out.php?link=http://www.example.com 

    http://example.biz/go/to/dir/exit.php?hello=aHR0cDovL3d3dy51cDEwMC51cy8xMDc2NzA= 

     http://links.example.biz/go/to/dir/exit.php?redirect=aHR0cDovL3d3dy51cDEwMC51cy8xMDc2NzA=

     

    Your time and effort is much appreciated and I'm looking forward for your help.

     

    Best Regards,

    M.A.G

  •  07-31-2010, 6:27 AM 70313 in reply to 70311

    Re: Hard/Easy Regular Expression help

    Hi Mag,

    This is a common question. Check out http://regexlib.com/DisplayPatterns.aspx?cattabindex=1&categoryId=2 for lots of examples

    Regards, Tom Pester

  •  08-01-2010, 7:44 PM 70345 in reply to 70311

    Re: Hard/Easy Regular Expression help

    Also, please remember that a regex is generally NOT the best way to process a URL. For example, your rules would exclude just about any non-US domain name as it only allows for a domain name with must have exactly 3 parts and most countries have a country suffix as well - e.g. www.optus.com.au is a well-known telco here is Australia. Having said that, if you have specified exactly what makes a valid URL for your situation then that is fine, but your middle 2 examples of the second part of your question don't match this requirement.

    For the first part, try:

    http://\w+\.\w+\.\w+/\d+/?

    and for the second part, try:

    http://\w+\.\w+\.\w+/[\w/]+\.php\?\w+=[\w./:]+

    You may want to set the "ignore case" option and will probably want to add in some capturing groups. Also, I've tried to interpret some of your requirements (as in "foldername maybe having a folder name or not" as meaning it is made up of alphanumeric and slash characters) but you may need to work on this a bit.

View as RSS news feed in XML