Today I’ll be following my previous post on metacharacters to give one more metacharacter as well as expand on what Regular Expressions are. It’s a basic overview of it but I think it’s helpful to both write about it as I learn it in a simplified way that may help others understand it better.
First, lets continue with metacharacters. Monday I talked about dot, caret, and dollar. Today I’ll finish up with star! Using (*) star allows you to search for a repetitions of either a character, class, or group. Say we want to search for eggy and any repetition of the group(bacon). How do we use star? If we look below, we see that when we define the pattern, we are searching for “eggy(bacon)*”. This tells python, search for “eggy” and any repetition of bacon from 0 onwards. So eggy, eggybacon, eggybaconbacon would all match. Easy peasy lemon squeazy right?
Now for the main course. Regular Expressions. What are they and why do we use Metacharacters in them?
Regular Expressions are part of a module in Python. They allow us to manipulate all types of strings and are popular in verifying strings match a particular pattern or to substitute something in a string. A good way to visualize their uses are search bars, log in verification, and switching out words in a string. Alone they are pretty useful but when combined with metacharacters allow us to be more specific and precise in what we need our code to do.
The main 5 Regular Expressions used that I will talk about are match, search, find all, find, and replace. First, lets go over how to get our module in. If we know it is a module in Python, the first thing we need to do is import it into our code.
After we have our Regular Expressions module imported we can begin. In using regular expressions, we need to define a pattern. Let us say we want to get a match for “bleep”. The format we use is pattern = r“”
Now that we have our pattern, we need to tell our code what to do if it finds it. Using match, we use and if/else statement. When we call on a regular expression such as match, we use a format of re.(whatever regular expression we need) as seen below and use 2 parameters. Pattern which tells what we are looking for and the string we are looking in. In this case “bleepbeepimmasheep”.
If we run it now, we get “Sheep found!” in our terminal.
However, say our string we are checking a match against is just “beepbleepimmasheep”
Because our string did not start with “bleep” it will not be considered a match and will tell us so. Match is specific like that. But! What if we use re.search?
It will match up because re.search looks through the entire string.
Just a little change can change your results and how you apply it in your code. In the next post I will go over the last 3 of the main regular expressions, I hope you look forward to it!