Python Project — Regex Query Tool
A regex query tool is a software utility designed to help create, test and execute regular expressions. Regular Expressions are sequences of characters that form search patterns and are primarily used for string matching and manipulation.
Need for a Python Regex Query Tool
There is a need to develop a regex query tool because working with regular expressions entails intricate work that demands precision. Regex patterns are expressed in a cryptic, specialised format that is difficult to correct for more complex cases. Mistakes in the use of regular expressions (regex) are likely to result in incorrect results when analysing data, and it may require a significant amount of resources to identify the problem. Hence, a regular expression (regex) query tool is needed.
About Python Regex Query Tool
This Python project aims to develop a user-friendly regular expression (regex) query tool. The tool will allow users to input and test regular expressions against various sample texts.
Prerequisites for Python Regex Query Tool
To run this project, the following prerequisites must be met:
- Python: The user must have Python installed on their system.
- Understanding of Regex: A solid grasp of regex syntax and operations.
- tkinter Library: This library is used to make a Windows GUI.
Step by Step Explanation of the Code
The following set of Python code assists in developing a basic GUI application in the Python language using the Tkinter toolkit for a regular expression (regex) query tool.
Get Rahul Patodi’s stories in your inbox
Join Medium for free to get updates from this writer.
Here is a detailed explanation of the code:
Importing Necessary Libraries
import tkinter as tkin
from tkinter import ttk
from tkinter import scrolledtext
import re- tkinter: It is used to create the application window
- ttk: It provides access to the Tk themed widget set.
- scrolledtext: It creates scrollable text areas for entering the text to search.
- re: It is used to compile and search for patterns within text.
Class Definition and Initialisation
class RegexQueryTool:
def __init__(self, root):
self.root = root
self.root.title("Regex-Query-Tool")
self.create_widgets()The ‘RegexQueryTool’ class contains all the functionality of the regex query tool. The constructor initialises the main window and sets its title. It is also called the ‘create_widgets’ method, which sets up the user interface.
Creating Widgets
def create_widgets(self):
self.regex_label = ttk.Label(self.root, text="Enter Regex Pattern:")
self.regex_label.grid(column=0, row=0, padx=10, pady=5, sticky=tkin.W)
self.regex_entry = ttk.Entry(self.root, width=50)
self.regex_entry.grid(column=1, row=0, padx=10, pady=5)
self.text_label = ttk.Label(self.root, text="Enter Text to Search:")
self.text_label.grid(column=0, row=1, padx=10, pady=5, sticky=tkin.W)
self.text_area = scrolledtext.ScrolledText(self.root, width=50, height=10, wrap=tkin.WORD)
self.text_area.grid(column=1, row=1, padx=10, pady=5)
self.search_button = ttk.Button(self.root, text="Search", command=self.search_regex)
self.search_button.grid(column=1, row=2, padx=10, pady=10, sticky=tkin.E)
self.result_label = ttk.Label(self.root, text="Results:")
self.result_label.grid(column=0, row=3, padx=10, pady=5, sticky=tkin.W)
self.result_area = scrolledtext.ScrolledText(self.root, width=50, height=10, wrap=tkin.WORD)
self.result_area.grid(column=1, row=3, padx=10, pady=5)The ‘create_widgets’ function helps create the labels and input boxes for Regex Pattern input, Text to Search Input, Search Button, and Results Display.
Search Function
def search_regex(self):
pattern = self.regex_entry.get()
text = self.text_area.get("1.0", tkin.END)
self.result_area.delete("1.0", tkin.END)
try:
match_phrase = re.findall(pattern, text)
if match_phrase:
for match in match_phrase:
self.result_area.insert(tkin.END, match + "\n")
else:
self.result_area.insert(tkin.END, "No match_phrase found.")
except re.error as e:
self.result_area.insert(tkin.END, f"Regex error: {e}")This method helps search for words or phrases in the input text according to the provided regular expression and displays the results in the output section.
Starting the Main Event Loop
if __name__ == "__main__":
root = tkin.Tk()
app = RegexQueryTool(root)
root.mainloop()It helps create the main window and starts the Tkinter event loop, which waits for user interaction.
Python Regex Query Tool Output
- This is the initial state of the Regex Query Tool application upon launch. The window contains three labeled entry fields: one for the regex pattern, one for the text to search and one for the results. The “Search” button is displayed below the entry fields, ready for the user to input the text.
2. The user has entered a regex pattern sentence into the first entry field. The entry field labeled “Enter Text to search” is filled with the user’s input, and the “Search” button is now ready to be clicked to process this text.
3. In this step, the user has clicked the “Search” button after entering the text. The application is processing the request. The entry fields remain visible, but the output text field remains empty while the request is processed.
4. After processing the text, the application displays the output text in the second field, labelled “Results”. The user can now use this text as needed.
Summary
The regex query tool is essential for anyone working with regular expressions. In this project, we are developing a tool using Python and Tkinter that helps users understand regular expressions. The above code yields an accessible and user-friendly regex query tool.










