Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Regex Query Tool

Here are examples of a regex query tool you can use for inspiration:

Here are resources that you can use to build your regex query tool:

00:00 Regex Query Tool. We deal with texts on a daily basis. Generally, that text will have a structure—such as lines, punctuation, and paragraphs—and sometimes, you’ll need to extract information from that text, whether it’s indicated by the structure or by the information itself, such as being a specific type of character, digits, punctuation, or a combination of words.

00:23 Using the regular search tool in most text editors can be ineffective for this kind of task. This is where regular expressions come in. Regular expressions are a rich language that allows a selection of a wide range of characters or structures within text.

00:38 Regular expressions have rules and structure, and a regex query tool will check for validity and show the user how the regex will work on a test string. With an appropriate regular expression query tool, users can quickly check whether their regular expressions work and correct any issues found on the test text quickly and efficiently.

00:58 Let’s look at a couple of implementations of regex query tools. Here, we see RegexTester.com. As you can see, a test string has been entered with three different phone numbers, two of which are in US format, one of which is in UK format. And placing in a regular expression, we can see that the top and bottom one have been highlighted appropriately, because this regex is designed to match US-format phone numbers.

01:24 Performing the same test on Regex101.com gives a similar but slightly different result. As you can see, the top and bottom numbers are still being highlighted, but we get an explanation on the right-hand side of the screen of how the regex works, and we get colored highlighting to show how the regular expression maps onto the test string that’s being captured.

01:45 Now, let’s look at some of the technical aspects you’ll need to overcome to implement this tool. Firstly, and rather obviously: regular expressions. re is part of the Python standard library and gives access to regular expressions with a standard and well-documented API.

02:02 There are many examples of how to use it across the web, and you will be able to get up to speed with this pretty quickly. Secondly, output selection. As we’ve seen, there’s a variety of ways you can choose how to format the output, and choosing this will involve spending some time working with HTML and CSS, and possibly using some JavaScript as well.

02:24 Now, let’s look at some extra challenges you could take on after programming your first version of the regex query tool. Firstly, dissecting expressions.

02:33 Sites such as Regex101 provide an explanation of how a given expression is working, and this will involve checking that an expression is valid, as well as displaying the individual elements of it and how they apply in this particular situation. Secondly, alternative suggestions.

02:49 If you take a look at any discussion online about regular expressions, even one as simple as extracting phone numbers, as seen here, you will see there are as many ways to do it as there are people taking part in the discussion.

03:00 Formulating an alternative expression generator would be an involved task, but it would allow you to gain an in-depth knowledge of regular expressions in the process.

Become a Member to join the conversation.