Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. realizing a URL using Regular Expressions

realizing a URL using Regular Expressions

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rizwan Rathore
    wrote on last edited by
    #1

    Hi All, I am developing a search engine and i m using "." as a seperator of a token..... but now i have to search URLs as well so anyone plz tell me how can i realize that a particular string is a URL..... looking forward for help Regards,

    E 1 Reply Last reply
    0
    • R Rizwan Rathore

      Hi All, I am developing a search engine and i m using "." as a seperator of a token..... but now i have to search URLs as well so anyone plz tell me how can i realize that a particular string is a URL..... looking forward for help Regards,

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      Have a look through regexlib[^] and you'll probably find several ones.  Another one is the RegularExpressionValidtorControl in ASP.NET has some built in ones, it may be in there.


      You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

      R 1 Reply Last reply
      0
      • E Ed Poore

        Have a look through regexlib[^] and you'll probably find several ones.  Another one is the RegularExpressionValidtorControl in ASP.NET has some built in ones, it may be in there.


        You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

        R Offline
        R Offline
        Rizwan Rathore
        wrote on last edited by
        #3

        Sir i want to do this using C# and want to know about the RegularExpression class of C#.... any help in this regard will b welcomed looking forward for help Regards,

        E 1 Reply Last reply
        0
        • R Rizwan Rathore

          Sir i want to do this using C# and want to know about the RegularExpression class of C#.... any help in this regard will b welcomed looking forward for help Regards,

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          Rizwan Rathore wrote:

          want to know about the RegularExpression class of C#

          Best place is the documentation provided by MSDN[^] Ok basically you want the System.Text.RegularExpression namespace, the most important class is RegEx which handles all the matching etc.  There is a constructor for this class which takes the regular expression pattern (for the URL in this case) and creates a RegEx object based on it.  If you then want to check for URLs you can use:

          • IsMatch - Checks if a string, or part of the string specified in the parameters matches the regular expression provided in the constructor.
          • Match - Returns a match object which contains the first match that the class found matching the regular expression.
          • Matches - Returns a collection of Match objects for each regular expression matched correctly.

          If the regular expression is enclosed in parentheses then the url can be extracted by using the Group collection. Example: This simple code will extract all the urls from a given string.

          // Must include: System.Text.RegularExpressions namespace

          string urls = "there is a url included somewhere in this string : http://www.codeproject.com/script/comments/forums.asp?msg=1524600&forumid=1649#xx1524600xx and here is another url http://regexlib.com/Search.aspx?k=uri";
          string urlRegex = @"(?<url>(mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)";

          Regex regex = new Regex(urlRegex);

          foreach (Match m in regex.Matches(urls))
          {
              Console.WriteLine(m.Groups["url"].Value);
          }
          Console.ReadLine();


          You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

          R 1 Reply Last reply
          0
          • E Ed Poore

            Rizwan Rathore wrote:

            want to know about the RegularExpression class of C#

            Best place is the documentation provided by MSDN[^] Ok basically you want the System.Text.RegularExpression namespace, the most important class is RegEx which handles all the matching etc.  There is a constructor for this class which takes the regular expression pattern (for the URL in this case) and creates a RegEx object based on it.  If you then want to check for URLs you can use:

            • IsMatch - Checks if a string, or part of the string specified in the parameters matches the regular expression provided in the constructor.
            • Match - Returns a match object which contains the first match that the class found matching the regular expression.
            • Matches - Returns a collection of Match objects for each regular expression matched correctly.

            If the regular expression is enclosed in parentheses then the url can be extracted by using the Group collection. Example: This simple code will extract all the urls from a given string.

            // Must include: System.Text.RegularExpressions namespace

            string urls = "there is a url included somewhere in this string : http://www.codeproject.com/script/comments/forums.asp?msg=1524600&forumid=1649#xx1524600xx and here is another url http://regexlib.com/Search.aspx?k=uri";
            string urlRegex = @"(?<url>(mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)";

            Regex regex = new Regex(urlRegex);

            foreach (Match m in regex.Matches(urls))
            {
                Console.WriteLine(m.Groups["url"].Value);
            }
            Console.ReadLine();


            You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

            R Offline
            R Offline
            Rizwan Rathore
            wrote on last edited by
            #5

            Hi Sir, thats exactly wat i need but when i tried the program i got this exception

            An unhandled exception of type 'System.ArgumentException' occurred in system.dll

            Additional information: parsing "(?(mailto\(news|(ht|f)tp(s?))\://){1}\S+)" - Too many )'s.

            at this instruction

            string urlRegex = @"(?(mailto\(news|(ht|f)tp(s?))\://){1}\S+)";

            now i am totally new commer in C# so plzz tell me how can i correct this i am writing the same code which u sent... so plzz reply back soon looking forward for help Regards, -- modified at 14:30 Friday 9th June, 2006

            E 1 Reply Last reply
            0
            • R Rizwan Rathore

              Hi Sir, thats exactly wat i need but when i tried the program i got this exception

              An unhandled exception of type 'System.ArgumentException' occurred in system.dll

              Additional information: parsing "(?(mailto\(news|(ht|f)tp(s?))\://){1}\S+)" - Too many )'s.

              at this instruction

              string urlRegex = @"(?(mailto\(news|(ht|f)tp(s?))\://){1}\S+)";

              now i am totally new commer in C# so plzz tell me how can i correct this i am writing the same code which u sent... so plzz reply back soon looking forward for help Regards, -- modified at 14:30 Friday 9th June, 2006

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #6

              Use this regular expression: (?<url>(mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)

              Rizwan Rathore wrote:

              so plzz reply back soon

              Be warned that some here think this is impolite, I'm not that fussed but others are because we're donating our free time to help you fix your problems.

              Rizwan Rathore wrote:

              looking forward for help

              Aren't we all :-D


              You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

              R 1 Reply Last reply
              0
              • E Ed Poore

                Use this regular expression: (?<url>(mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)

                Rizwan Rathore wrote:

                so plzz reply back soon

                Be warned that some here think this is impolite, I'm not that fussed but others are because we're donating our free time to help you fix your problems.

                Rizwan Rathore wrote:

                looking forward for help

                Aren't we all :-D


                You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                R Offline
                R Offline
                Rizwan Rathore
                wrote on last edited by
                #7

                Sorry if u mind that i will avoid that in future but sir i was using exactly same regular expression wich gave exception :( so wot shud i do now :(???

                E 1 Reply Last reply
                0
                • R Rizwan Rathore

                  Sorry if u mind that i will avoid that in future but sir i was using exactly same regular expression wich gave exception :( so wot shud i do now :(???

                  E Offline
                  E Offline
                  Ed Poore
                  wrote on last edited by
                  #8

                  The one I gave in the previous reply works fine on my machine, remember to write the string in code as @"regex" rather than "regex" since C# will try and escape the \ characters.


                  You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups