Examples of using Python Regular Expressions: python Regular Expressions
As a concept, regular expressions are not unique to Python. However, regular expressions in Python are slightly different in actual use.
This article is part of a series of articles about Python regular expressions. In the first article of this series, we will focus on how to use regular expressions in Python and highlight some unique features in Python.
We will introduce some methods for searching and searching strings in Python. Then we will discuss how to use grouping to process the child items of the matching objects we find.
The module of Regular Expressions in Python that we are interested in using is usually called 'Re '.
>>> import re
1. Original Type strings in Python
The Python compiler uses '\' (backslash) to indicate escape characters in string constants.
If the backslash is followed by a string of special characters that can be recognized by the compiler, the entire escape sequence will be replaced with the corresponding special characters (for example, '\ n' will be replaced by a linefeed by the compiler ).
This causes a problem when using a regular expression in Python, because the 'Re' module also uses a backslash to escape special characters (such as * and +) in the regular expression ).
The combination of the two methods means that sometimes you have to escape the Escape Character itself (when the special characters can be recognized by the Python and regular expression compilers at the same time ), however, you do not have to do this in other cases (if special characters can only be recognized by the Python compiler ).
Instead of thinking about how many backslashes are needed, we can use the original string instead.
A string of the original type can be simply created by adding a character 'R' before the double quotation marks of a common string. When a string is of the original type, the Python compiler will not try to replace it. Essentially, you are telling the compiler not to interfere with your string at all.
>>> string = 'This is a\nnormal string'>>> rawString = r'and this is a\nraw string'>>> print string
This is a normal string
>>> print rawStringand this is a\nraw string
This is a string of the original type.
Search Using Regular Expressions in Python
The 'Re' module provides several methods for exact query of input strings. We will discuss the following methods:
re.match()re.search()re.findall()
Each method receives a regular expression and a string to be matched. Let's take a closer look at each of these methods to figure out how they work and how different they are.
2. Use re. match to search-match to start
Let's take a look at the match () method first. The match () method works only when the start of the searched string matches the pattern.
For example, to call the mathch () method for the 'dog cat dog' string, the search mode 'Dog' will match:
>>> re.match(r'dog', 'dog cat dog')<_sre.SRE_Match object at 0xb743e720<>>> match = re.match(r'dog', 'dog cat dog')>>> match.group(0)'dog'
We will discuss more about the group () method later. Now, we only need to know that we have called it using 0 as its parameter, and the group () method returns the matching pattern found.
I have skipped the returned SRE_Match object for the moment, and we will discuss it soon.
However, if we call the math () method for the same string and find the 'cat' pattern, the match will not be found.
>>> re.match(r'cat', 'dog cat dog')>>>
3. Use re. search to find and match any location
The search () method is similar to match (), but the search () method does not limit us to search for matching only from the beginning of the string, therefore, finding 'cat' in our sample string will find a match:
search(r'cat', 'dog cat dog')>>> match.group(0)'cat'
However, the search () method will stop searching after it finds a match, so we use searc () in our sample string () method to find the 'dog 'only find its first appearance location.
>>> match = re.search(r'dog', 'dog cat dog')>>> match.group(0)'dog'
4. Use re. findall-all matching objects
Currently, the most frequently used search method in Python is the findall () method. When we call the findall () method, we can easily get a list of all matching modes, rather than get the match object (we will discuss more about the match object next ). This is easier for me. Call the findall () method for the sample string. We get:
['dog', 'dog']>>> re.findall(r'cat', 'dog cat dog')['cat']
5. Use the match. start and match. end methods.
So what is the 'match' object previously returned to us by the search () and match () methods?
Different from the matching part of a simple returned string, the "matching object" returned by search () and match () is actually a packaging class for matching substrings.
Previously, you can call the group () method to obtain the matched substring. (We will see in the next section that the matched object is actually very useful when dealing with grouping issues ), however, the matching object also contains