Heyyy, How is it going? Welcome to another CRAZY Python lesson! 

Today we will be learning about Regular Expressions. This is one of the MOST important topics you will ever learn in programming. It will help you in a lot of stuff like automation, web scraping, data science, machine learning……basically everything! So, let’s not waste any more time and learn about Regular Expressions…

What are Regular Expressions?

A Regular expression is a sequence of characters that define a search pattern. Basically, it helps you search a chunk of data. But this is not some regular searching like you would do on Google, it is similar but not exactly it. It uses a search pattern. You will understand the difference in just a second.

To learn about regular expressions we will be using a poem which comes with your python installation (Yeah I know….weird). The poem is called “The Zen Of Python”. To get this poem you must execute the following line in your command prompt:

python –c “import this” 

Note: This command will probably not work if you haven’t set up Python in your command prompt. Do not worry, I have already created an article on how to set up cmd to execute python commands here.

Now copy the poem and paste it in a new ‘txt’ file and name it “Zen Of Python”.

USING A REGULAR EXPRESSION TO PERFORM A SEARCH

  1. IMPORTING THE ‘RE’ MODULE

Create a new ‘py’ file and store it in the same directory as ‘Zen Of Python.txt’.

In order to search the poem, you must have a third-party module installed in your system named ‘re’. You can install it like any other module using this guide. 

Once installed, import it into your program.

  1. GATHERING THE CONTENT TO BE SEARCHED

Now, we will read the text file using python and then search some text in it. If you want to learn more about how to use Python to read a file, then simply follow this article. Here is the code to read an article and store it in a variable:

with open(‘Zen Of Python.txt’, ‘r’) as f:

poem = f.read()

Now the variable ‘poem’ contains the entire poem.

  1. DEFINE A REGEX

‘regex’ is short for REgular EXpression. You’ll be designing your search pattern that you want to use when searching the content.

Use the following code to define a search pattern:

regex = re.findall(r’Beautiful’, poem, re.IGNORECASE)

The above search pattern will find all the instances of the word ‘beautiful’ in the poem. 

The ‘findall()’ method accepts two mandatory parameter and one optional parameter. The first is the regular expression (i.e., what you are looking for), second is the string that you want to be searched and the optional parameter is any additional operation. In this case, we asked python to ignore the case of the string using ‘re.IGNORECASE’.

CONCLUSION

This was a basic introduction to regular expressions in python. There are tons of other stuff you can do with regular expressions in python, like search for digits, do a more detailed search with strings and much more. In this article, we simply scraped the surface and learnt how to define a regular expression. More detailed article on regular expression will follow soon which will make you a master of regular expressions.

Give us a LIKE 👍 and FOLLOW, if you like our content and want more. We won’t let you down😉

If you have a doubt, query or any feedback, I would love to learn about it in the comments below👇

You can chat with me and other programmers from our community by joining our telegram group Follow the link and become an important and valued part of our programming community today. See you there!!!

I’ll be back with some new knowledge very soon, until then…..

    HAVE AN AMAZING DAY !!!!