URL stands for Uniform Resource Locator. A URL is the global address for a document or resource on the internet. A sample URL is given below.
http://csharp.net-informations.com
The following C# program checking whether a URL exist in the string or not. Here we are using Regex.IsMatch(inputString,patternString) method to find the URL in a string
using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
string pattern = null;
string urlStr = "http://net-informations.com";
pattern = "http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?";
if (Regex.IsMatch(urlStr, pattern))
{
MessageBox.Show ("Url exist in the string ");
}
else
{
MessageBox.Show("String does not contain url ");
}
}
}
}