

Metacharacter is a character with the specified meaning. Metacharacters are characters which are interpreted in a particular way.

Regular Expression in Python and Their Uses Metacharacters So within flower power with the pattern of flower, the pattern that was identified was flower. Now, that match object is going to have some helpful methods to help us figure out where the match occurred.įor example, on my match object, I can call a method called group and group is going to return the actual string that’s matched. So we’re going to get a match object right here on the right hand side. So now this combination of six characters that we specified in here is going to exist at some point in this string.

So what I’m going to do below is I’m going to once again invoke the search method on my pattern object and I’m going to give it a string like flower power. We’re going to see it’s going to be the none object whenever Python cannot find a match using the regular expression pattern and returns None. So, again, Python and regular expressions is going to look for this combination of characters flower within this string of candy. First up, let’s pass in a string like candy. Let’s take a look at both of those scenarios. And if the pattern does match in the string that we pass in, we’re going to get a different type of object called a match. If the pattern does not exist, we’re going to get a none object to represent nowness or nothingness. To work with regular expressions will have to begin by importing a module from within the standard library called re. So regular expressions are just an internal language built into Python that allows us to identify and write out those strategies to help identify snippets of text within larger chunks of text. Or if we’re looking for something like a zip code within the United States, we can write a pattern to search for five digits in a row. And usually, those numbers are separated by spaces or dashes or slashes or something like that. A phone number has a specific pattern as well. Or we can, for example, be looking for a phone number. We have the sign in the middle, and then we have something before it and something afterward. An email address has a particular pattern. It’s a strategy to identify text, and the applications in the real world are vast.įor example, we may need to pass a big chunk of text and find the nested email address within it. So a pattern can mean something like three digits in a row or two alphabetic letters in a row or the letters BCA in sequence, or any number of whitespace characters in a row. It’s just a strategy that we use to identify text.
