Regular Expression Checker in Python

NeuralNine · Intermediate ·🖌️ UI/UX Design ·2y ago

Key Takeaways

The video demonstrates how to build a regular expression checker in Python with a graphical user interface (GUI) using TKinter and regex, covering UI design and regular expression matching.

Full Transcript

what is going on guys welcome back in this video today we're going to learn how to build a regular expression Checker in Python so let us get right into [Music] it all right so what we're going to do first is we're going to take a look at the final result as a motivational preview upfront also please excuse my voice today and occasional voice cracks I'm a bit cold so it might sound a little bit unusual today uh but this is what we're going to end up with a regex tool we have a text entry for regx a text entry for the text itself and we can find matches and we can check for a match so here I have prepared some text and I also prepared a regular expression and the idea is now that if I find matches it's going to highlight the matches for example here and here and when I type uh when I click on check match it's going to check if the whole text matches uh this rag X and if I remove all the stuff that does not match the rag X here if I just say uh check match it will tell me regx matches the whole text all right so let us get right into the implementation we're only going to use core python today we don't need to rely on any external packages and we're only going to have two Imports first of all import re e which is for the regular expression checking and then also import TK enter as TK for the graphical user interface and what we're going to do first is we're going to Define two functions the first one is going to be called FInd matches uh we're going to pass for now and we're going to have a second one which is going to be check match and we're going to pass here as well and then we're going to define the basic graphical user interface we're going to say root equals TK TK we're going to set a title which is going to be regex Checker or something like this uh and then we're going to add some basic UI elements that we saw in the preview so we're going to say here uh regor label is going to be tk. label and the label will have uh will be part of the root so it will have root as a parent element and the text is going to just be enter regex um without without a space here and then we're going to use a grid layout to place all of this into the root uh window so regex label. grit we're going to place this in row zero in column zero with a padding X of five and a padding y of five um and in the end of course we're going to do root main Loop so already we should have a graphical user interface with uh a simple label here now uh what we're going to do next is we're going to add the text entry so we're going to say regex uh entry is going to be equal to tk. entry it's going to be part of the root element and it's going to have a width of 50 and then we're going to use again the GD method to place this uh in the same row but not in the same column so still row zero column one um and the padding is going to be the same so padding X and padding y are going to be equal to five and then basically what we can do is we can copy this and we can slightly change this to be not regex label but text label and also text entry and what we want to do here is enter text and this of course will be Row one and this will be Row one and one more difference here is we're going to have a height of 10 here um is this correct oh actually this is a text not an entry so it looks like this now we have a text entry and we have a text area in addition to that now we're going to add two buttons the find button which is going to be a TK button which is going to be part of root we'll have the text FInd matches and will be linked to the function FInd matches from above which we have to still implement this one here um and then we're going to say find button. Grit the row is going to be equal to two the column is going to be equal to zero the column span is going to be equal to two so spans two columns uh petting X is going to be five pading Y is going to be five and we're going to make it sticky to West and East so sticky we then we're going to copy this we're going to paste this we're going to replace find by uh check and we're going to say check check match the command here is going to be check match and we're going to change this to row three and finally we want to have a result label which is going to be actually let me just copy this here this result label will have the text uh or actually in the beginning it won't have any text at all it will have a foreground which is going to be green by default and we're going to place this at Row 4 column zero column span equals 2 and that's it so this is our UI now and the only thing that we need to do now is we need to implement we need to implement the FInd matches and check match functions so for FInd matches it's actually not too complicated or actually both of them are not very complicated we basically just say that we want to have a regx object which is going to be equal to regx entry uh doget so it's going to be the content of this entry here it's just going to be the regular expression itself um and we also want to have the text and the text is basically the text entry doget but since this is now a text and not an entry field um we need to specify a starting point 1.0 and an end point and as an end we're going to use end minus one character minus one C to get rid of the last uh line break uh this is just how you select all the text and then we're going to say now uh if there is a regex and a text so if both are um if both are actually uh if both have some content what we do is we get all the matches by saying R find itter and we're going to pass here the regex and the text and what we do then actually we don't need this uh what we do then is we iterate over the matches for match in matches uh we say that the starting index of the respective match so you can have multiple matches but for this particular one the start index is the following 1.0 which is the starting location plus a certain number of uh characters we're going to make this an F string here and the certain number of characters will be match. start and we just add Char so this string basically says from the beginning where does the match start that many characters offset and the end index will be the same with match end uh and then we can say text entry tag at because now to this section we want to add a styling so we're going to say match will be added as a tag to start in index or from start index to end index and we're going to say now that the text entry tag config will be configured in a way that match has a certain styling and The Styling is that the foreground is going to be uh black and so basically the text is going to be black and the background is going to be red and the font is going to be whatever let's pick areial 10 and what did I pick B like this so that is basically defined matches function so this should already work if we go ahead and do the same thing again so we have this here and we have this here you can see that this already works now so the only function we need to implement is the check match function so what we're going to do here now is we're going to say again the regular expression is going to going to be whatever we get from the text entry or from the reix entry the text is going to be equal to text entry get from 1.0 until end minus one character and if regex and text are provided if it is a match the regex matches the text if that is the case all of the text um then we're going to say result label. config and I'm going to set the text equal to regx matches the whole text foreground is going to be equal to Green otherwise we're going to copy this regex does not match the whole text foreground equals R now we can do the same thing FInd matches Works check match says it's not a match now we can get rid of some stuff here and there you go matches old text so this is how you implement a regx checker with a graphical user interface in Python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below also don't forget to subscribe to this Channel and hit the notification Bell to not a single future video for free other than that thank you much for watching see you in the next video and bye

Original Description

Today we build a regular expression (RegEx) checker in Python with a graphical user interface (GUI). ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💼 Services 💼 💻 Freelancing & Tutoring: https://www.neuralnine.com/services 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to build a regular expression checker in Python with a GUI, covering UI design and regex matching. It demonstrates how to create a basic GUI, implement regex matching, and display matches. The practical applications of this skill include building tools for text analysis and processing.

Key Takeaways
  1. Define two functions: Find matches and Check match
  2. Create a basic GUI with label, text entry, and buttons
  3. Use grid layout to place UI elements
  4. Add text area in addition to text entry
  5. Create buttons for Find matches and Check match
  6. Configure text entry tag to display matches
  7. Implement Find matches function
  8. Implement Check match function
  9. Set text equal to regex match of whole text
  10. Set text color to green if regex matches whole text
💡 The video highlights the importance of UI design in building effective tools for text analysis and processing, and demonstrates how to integrate regex matching with a GUI in Python.

Related AI Lessons

A Young Designer’s Question: What Are Companies Actually Hiring For?
Learn what companies are actually hiring for in UX design and how to position yourself for success
Medium · UX Design
Why Clear Calls-to-Action Are Essential for Business Websites
Learn why clear calls-to-action are crucial for business websites to convert visitors into customers
Medium · UX Design
AI in Design: The Skill That Gets Scarce When Making Gets Cheap.
Learn how AI is changing the design industry by making creation cheaper and more abundant, shifting the designer's role to curator and decision-maker
Medium · AI
Sheba Manager Mobile Apps: Retail OS in the Palm of a Merchant’s Hand
Learn how a Sr. Product Designer revamped the Sheba Manager Mobile app to create a retail OS for merchants
Medium · UX Design
Up next
Answering Revit Questions for Reddit
Balkan Architect
Watch →