Home >Backend Development >Python Tutorial >Detailed explanation of regular expressions in python (example analysis)

Detailed explanation of regular expressions in python (example analysis)

乌拉乌拉~
乌拉乌拉~Original
2018-08-22 16:54:001186browse

In this article, we will introduce in detail the advanced knowledge about regular expressions, and I will write down some problems that may arise in python programming. Examples of regular expressions.

The first advanced knowledge point:

##Split string

Using regular expressions to split strings is more flexible than using fixed characters. Please see the normal splitting code:

>>> 'a b   c'.split(' ')
['a', 'b', '', '', 'c']

Well, continuous spaces cannot be recognized. Try using regular expressions:

>>> re.split(r'\s+', 'a b   c')
['a', 'b', 'c']

No matter how many spaces there are, it can be divided normally. Join, try:

>>> re.split(r'[\s\,]+', 'a,b, c  d')
['a', 'b', 'c', 'd']

Join again; try:

>>> re.split(r'[\s\,\;]+', 'a,b;; c  d')
['a', 'b', 'c', 'd']

If the user enters a set of tags, remember to use regular expressions to convert irregular input into correct ones next time array.

Grouping

In addition to simply determining whether to match, regular expressions also have the powerful function of extracting substrings. What is represented by () is the group to be extracted. For example:

^(\d{3})-(\d{3,8})$ defines two groups respectively, which can be extracted directly from the matching string Area code and local number:

>>> m = re.match(r'^(\d{3})-(\d{3,8})$', '010-12345')
>>> m
<_sre.SRE_Match object; span=(0, 9), match=&#39;010-12345&#39;
>>>> m.group(0)
&#39;010-12345&#39;
>>> m.group(1)
&#39;010&#39;
>>> m.group(2)
&#39;12345&#39;

If a group is defined in the regular expression, you can use the group() method on the Match object to extract the substring.

Note that group(0) is always the original string, group(1), group(2)... represents the 1st, 2nd,... substring.

Extracting substrings is very useful. Let’s look at a more brutal example:

>>> t = &#39;19:05:30&#39;
>>> m = re.match(r&#39;^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]
|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])$&#39;, t)
>>> m.groups()
(&#39;19&#39;, &#39;05&#39;, &#39;30&#39;)

This regular expression can directly identify legal time. But sometimes, regular expressions cannot be used to completely verify, such as identifying dates:

&#39;^(0[1-9]|1[0-2]|[0-9])-(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[0-9])$&#39;

for

'2-30', '4-31' Such illegal dates cannot be identified using regular expressions, or it is very difficult to write them out. In this case, a program is required to cooperate with the identification.

The above is all the content of this article. This article mainly introduces the knowledge related to regular expressions

in python. I hope you can use the information to understand the above. content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the

Python tutorial column on the php Chinese website.

The above is the detailed content of Detailed explanation of regular expressions in python (example analysis). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more