Module 3: Realtime Use Case of Regular Expressions for Beginners - Part 4
You might be wondering by knowing that you can also use regex for address matching, though there is a wide variance in address content and formatting, still, you can use regex to match it to some extent.
Before we write the regex, let's try to understand this sample address format. Here the first set consists of street number and street name, followed by a comma. There is a city name followed by a comma and then there is a state name abbreviation like in our case TX stands for Texas. Lastly, there is a zip code. Once you identify the pattern, it becomes easy to write the regex.
Now let's jump into the demo to write the regex for matching this home address. In the input text, I have added some sample addresses. First, write the expression to match the street number and street name slash w and complete the group. This backslash w is not matching space. Add space and comma in the character class. Repeat this group one or more times by adding plus signs. We can also match this city name using this same expression. Add this expression again into one group by adding parenthesis and repeat this expression two times.
Now write the regex expression to match this state abbreviation. The state abbreviation is always consisting of two characters. For that define one group and in the character, the class adds the range. Then repeated it two times, followed by comma and space. The last one is a zip code. For that, use backslash d in one group and repeat it one or more times and assert the end of the string. This is how we can create the working regex to match the address.
Now use this same regex in the PDF multi-tool to find the address in the PDF file. In the PDF multi-tool, I have loaded one sample PDF file which contains the address over here. Click on this link and copy our regular expression, which we have just created. When I click on this find button, now it is highlighting the matching address. Our created regular expression is correct. Let's move to the next demo.
Email address validation using regular expression is one of the most common tasks which may be required in any application which seeks email address as required information in the registration step or there may be more use cases. I think no need to explain the format of the email address as we all have our email addresses.
Let's jump into the demo to match the email address. Here I have added some sample email addresses in the input text. First, write a regex expression to match the user name or write the expression to match the character before the @ part. This username portion is consisting of capital A to capital Z. Define the character class and write the range over here and then digit. There can be an underscore or a dot or a hyphen. Repeat this character one or more times by adding plus sign characters over here. Add the @ character sign over here. After this, @ sign, there is a domain name.
For domain names, we need to write expressions, character class, and define the range in the character class. Copy it over here. In the domain name, there can be either dot or hyphen and same way repeat this character one or more times by adding plus sign character over here and add the DOT. Now to match the last part there can be either minimum of two characters and a maximum of four characters. For example(.com, .edu, .in etc). For that use this expression again in the character class. Define the range and limit the length to 2 to 4.
This is the working regex to match the email address. Find the email address in a PDF file using this same regex that we have created but in PDF multi-tool. In the PDF multi-tool, I have added one sample PDF file which contains the email address over here. Click on this link, which opens the pop-up, and copy our regular expression in these textbooks. When I click on the find next button, it is highlighting the valid email address. Another email address is also over here. This is how we can use our regex to find the valid email address in this tool.
We have reached the end of the module. I hope you will take the time to work through the demos for yourself. So you can see how all of this works. I hope you enjoy this course. Feel free to improve the regex we learned in the demo over here.
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