Learn PythonPythonPython GUIWindows

Learn How To Use Regular Expressions With Python In A Windows GUI App

screen shot 2021 01 15 at 5 45 32 pm

The strength of Delphi combined with Python gives its users the chance to fast track their development processes and deliver excellent applications with ease! This tutorial helps you accomplish just that. With Python4Delphi (P4D) you can create a GUI for your python scripts in a few simple steps! P4D is a collection of free components that wrap up the Python DLL into Delphi and C++Builder. This post will demonstrate how we can use Python Lists within the Delphi development environment to save our data and display it in the GUI. For this purpose, our approach will be to create a VCL application and run our script inside of it. The output will be visible as soon as the script is executed.

Prerequisites: Before we begin working, it is essential we Download the latest version of Python for your platform. You can find the installation instructions for Python4Delphi at this link. Alternatively, you can follow the easy instructions found in this video Getting started with Python4Delphi.

The project shared, i.e.  Demo1 is an application that shows how a Python Script can be run by entering Python code inside of a Memo. This application type is known as VCL and is available on GitHub at the following link. In order to run the Python script in Python for Delphi, download, open and run project Demo1. Next, insert the script into the lower Memo, click the Execute Script button, and you will see the resultant GUI (if any) in upper Memo. You will see a small window titled “Demo of Python” This window will have an upper and a lower Memo. Enter your Python script in the lower Memo and click execute. You shall see the results on the upper Memo soon after.

The implementation details of how Delphi manages to run your Python code behind the scenes and which code is executed in order to accomplish that can be found at this link.

The Python Code :

Regular Expressions are a powerful tool but can be tricky to get right. Most beginners struggle with grasping the concepts but we will explain this topic to you with a very simple demo. In order to use Regular Expressions, we must first import the module Regex into our python file. We can do that by entering “import re”

Now in order to understand regular expressions, we should first consider the different functions that are involved. The library re has the functions findall, search, split and sub. The fist one returns a list that contains all of the matches. The second one, i.e. search returns the object containing information about the search result. The third one, will return a new list. Each element of this new list will be created after splitting the original screen at each match. And finally sub, replaces our matches with another string.

But that is not all, apart from different functions, there are also meta-characters, special sequences and sets that we need to take into consideration. For example, the meta character ” [ ] ” is used to inclose a set of characters and ” ^ ” means starts with and ” $ ” means ends with. You can learn more about the different symbols over here in the Python RegEx documentation.

Without further delay, lets jump right into the demo!

The findall function returns a list containing all of the matches that were found in our string. Since we have searched for “o” and there are two of those in the list, we can expect the resultant list to be [ “o” , “o” ]. If no matches are found, it will return an empty list [].

The above code will search the string and look for “o” wherever it finds the first “o” it will stop right there and save the starting index as well as the matching index of the matched string in the variable y. Over here, if we start counting from the index 0, we can see that the first “o” is present at index 4. And since its length is 1, the ending index will be 5. By using y.start(), we are choosing to display only the starting index. If the code fails to find a match in this string, it will result in an error at y.start(). Can you guess why?

The above code is for Split and Sub. Let’s take a look at the results we get from all the above.

screen shot 2021 01 14 at 6 37 42 pm 8600594

Check out some more cool tutorials on Python4Delphi over here.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *