realizing a URL using Regular Expressions
-
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,
-
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,
-
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,
-
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,
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 isRegEx
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 ofMatch
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
-
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 isRegEx
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 ofMatch
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
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
-
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
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
-
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
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 :(???
-
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 :(???
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