Regular expression in python

In our last tutorial, we have learned about the open file to read and write the data. If you have not checked the I would be recommended please check the articled which is posted here link. Some of the others of related out python learning series are here
Understanding Exception Handling in python,  Python OOPS concept with Class, Object and Constructor,
concept of variablesdata typesconditional control, functional control,  Module, and packages and Data Structure (List, Dictionary, and Set) of python.

Now in this tutorial, we will learn about the Regular Expression(Regex). Regular Expression used for pattern matching. Regular expression use ‘\’ to avoid considering characters as regular expression keys. It is better to provide the pattern as raw string i.r r’<patrn>’.  A regular expression can contain special characters and normal characters. Let’s see what are the special characters are available in regex and what are the meaning of those in python? To execute any regular expression we need to import the internal module “re”.

I hope the above explanation is understandable. Let’s see by an example to better understanding.

Let’s see the example of ‘^’ matches.

Let’s see the example of matching *, +, ?

Let’s explore the same of other special characters which are used as the regular expression.

Let’s see the non-greed matching pattern by an example.

Now let’s move to some of the special symbols of regular expression. These symbols are most frequently used in daily program basis.

Regex: Special symbols

‘\nn’: matches the group number. Max 99
‘\A’: matches only the start of string
‘\b’: matches empty string at beginning and end of word
‘\B’: matches empty string, but not at beginning or end
‘\d’: matches any digit
‘\D’: matches any non-digit
‘\s’: matches any whitespace character. [ \t\r\n\f\v]
‘\S’: matches non-whitespace character
‘\w’: matches alphanumeric and underscore
‘\W’: matches non-alphanumeric and non-underscore
‘\Z’: matches end of string

Let’s see a few examples for the above regex matching.

You can much more practice of regular expression. I hope all the explanation is understandable.

Summary: Now we have a good understanding of the Regular expression of python. We have seen a few examples and play around it to clear the doubts. If you still having issue or doubts then please add a comment I will try to reply as much possible. In my next python tutorial series, we will learn about the Database with the example.

If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.

Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on the below section.

Happy coding 🙂

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.