2

I am looking for a way to parse url link into following segments without using System.Uri

/Default.aspx/123/test?var1=val1

I need to break down this url link into values:

  1. File
  2. PathInfo
  3. Querystring
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
Artem Gassan
  • 45
  • 1
  • 5
  • possible duplicate of [regular expression for url](http://stackoverflow.com/questions/833469/regular-expression-for-url) – Woot4Moo May 26 '11 at 01:52

3 Answers3

2

Here's one:

string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)"

Origin Link

sra
  • 23,820
  • 7
  • 55
  • 89
bryanbcook
  • 16,210
  • 2
  • 40
  • 69
  • 1
    it is a bad practice just posting links. If the link broke your answer says nothing – sra May 26 '11 at 05:39
  • 1
    fair enough. but you'd get the same answer by googling "regular expression url", to take credit for their work seems ...wrong. – bryanbcook May 27 '11 at 04:21
2

Here is my code:

   var match = Regex.Match(internalUrl,
                            @"^\/([\w|\/|\-|\,|\s]+)\.([a-zA-Z]{2,5})([\w|\/|\-|\,|\s]*)\??(.*)",
                            RegexOptions.IgnoreCase | RegexOptions.Singleline |
                            RegexOptions.CultureInvariant | RegexOptions.Compiled);
    if (match.Success)
    {
        var filePath = match.Groups[1].Value;
        var fileExtention = match.Groups[2].Value;
        var pathInfo = match.Groups[3].Value;
        var queryString = match.Groups[4].Value;

        log.Debug("FilePath: " + filePath);
        log.Debug("FileExtention: " + fileExtention);
        log.Debug("PathInfo: " + pathInfo);
        log.Debug("QueryString: " + queryString);
    }
Artem Gassan
  • 45
  • 1
  • 5
1
string pattern= "\b(?<protocol>https?|ftp|gopher|telnet|file|notes|ms-help)://(?<domain>[-A-Z0-9.]+)(?<file>/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(?<parameters>\?[-A-Z0-9+&@#/%=~_|!:,.;]*)?"

This will generate named groups check for for what you want to extract

Rajeev
  • 4,571
  • 2
  • 22
  • 35