C# URL Parsing

A URL, which stands for Uniform Resource Locator, serves as a global address for identifying a specific document or resource on the internet. It acts as a unique identifier, allowing users and applications to access and retrieve the desired information from any location worldwide.

Below is an example of a sample URL:


Regex.IsMatch(inputString, patternString) method

To find a URL within a given string, we can utilize the Regex.IsMatch(inputString, patternString) method, which is available in the .NET Framework. This method helps us determine whether a specific regular expression pattern matches any part of the input string.

By providing the appropriate regular expression pattern that represents the structure of a URL, we can use Regex.IsMatch to identify and validate URLs within a given text or input. This approach is particularly useful when parsing and extracting URLs from larger bodies of text or when enforcing URL validation in an application. The following C# program checking whether a URL exist in the string or not.

Full Source C#
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 = "https://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 "); } } } }


Using Regex.IsMatch method with the appropriate pattern empowers developers to effectively identify and work with URLs, enabling them to handle and process web-related information in a precise and reliable manner.