- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to validate a URL using regular expression in C#?
To validate, you need to check for the protocols.
http https
With that, you need to check for .com, .in, .org, etc.
For this, use the following regular expression −
(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?
The following is the code −
Example
using System; using System.Text.RegularExpressions; namespace RegExApplication { class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "https://example.com"; Console.WriteLine("Matching URL..."); showMatch(str, @"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?"); Console.ReadKey(); } } }
Output
Matching URL... The Expression: ^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)? https://example.com
- Related Articles
- How do you validate a URL with a regular expression in Python?
- How to validate an email id using regular expression in Python?
- How to write a Python Regular Expression to validate numbers?
- How to validate URL address in JavaScript?
- How to use Python Regular expression to extract URL from an HTML link?
- Validate URL in ReactJS
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- How to validate an email address using Java regular expressions.
- How to Split String in Java using Regular Expression?
- How to match a nonwhitespace character in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- How to find a matching substring using regular expression in C#?
- How to match only digits in Python using Regular Expression?
- Java regular expression program to validate an email including blank field valid as well

Advertisements