Module 3: Realtime Use Case of Regular Expressions for Beginners - Part 3
Previous Tutorial - Next Tutorial
You have given a requirement into your application to check whether a given piece of text is a valid URL or not. A valid URL consists of a scheme, either HTTP or HTTPS which is followed by a colon, double slash, and then followed by a valid domain name. I think there is no need to explain this URL format in detail.
Let's jump into the demo to write regex step by step to match the website URL. Here in the input text, I have added some website URLs. Write the Regex to match this website URL. Match this scheme first, there can be either http or https. Now define it in one group by using the parentheses followed by a colon and then there are two double slashes, either I can use a double slash or I can add it in character class and repeat it two times.
Now to match the domain name, we need to bear in mind that a valid URL can only contain letters, digits, hyphens, and dots. Write regex for that. Add it in one group and character class backslash w then there can be a hyphen or dot and repeat it one or more times by adding a plus character. Then add a dot over here. The last part of the URL consists of a minimum of two characters and a maximum of three characters to match (.com, .org, .edu, .in, etc).
For that define the character class and in the character class, it has defined the range then repeating it a minimum of two times and a maximum of three times. Now to match the last character, which is a forward slash, add this expression and this forward slash is optional. Add the question mark over here and then assert the end of the string. Now we have a working regex to match the URL.
What happens if I end the wrong URL. This expression is not matching this invalid URL. Find this website URL in a PDF file using the same regex, but using our PDF multi-tool. In the PDF multi-tool, I have already loaded one sample PDF file and it contained some sample website URL over here. Click on this link and copy over regex. Click on this find next button.
It is now highlighting the website URL over here. This is how we can match the website URL in the PDF using this tool.
Web API for developers Free Trial Offline SDK
Here's RegEx video tutorial:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C