Link Search Menu Expand Document

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.

Regular Expressions Use Cases

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.

Regular Expressions Use Case

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:

Back to top

© , — All Rights Reserved - Terms of Use - Privacy Policy