Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Python regular expression

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this "Python regular expression case Analysis" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python regular expression case Analysis" article.

I. the function of regular expressions

Tip: a regular expression is a special string that represents a piece of regular information. If we want to extract what we want from a paragraph of text, we can easily help us to extract it through regular expressions.

Tip: the following is the main body of this article. The following examples are available for reference.

II. Basic symbols of regular expressions

1. Dot "."

"." It means to match any character except newline characters, including but not limited to English letters, numbers, Chinese characters, English punctuation marks and Chinese punctuation marks.

2. Asterisk ""

"" matches a subexpression (normal character, another regular expression symbol or several regular expression symbols) before it 0 to an infinite number of times.

3. Question mark "?"

"?" Indicates that the subexpression in front of it is matched 0 or 1 times. Note that the question mark here is an English question mark.

4. The number "\ d"

"\ d" represents a number in a regular expression, and "\ d" consists of a backslash and the letter d, but treat "\ d" as a regular expression symbol as a whole.

5. Brackets "()"

Parentheses can extract the contents of parentheses.

3. Python has its own regular expression module

Python comes with a very powerful regular expression module called "re", which is the acronym of "regular expression". In Python, you need to use the import statement: import re to import this module before using it.

1.findall function

The function prototype of findall is: re.findall (pattern, string, flags=0)

Pattern represents regular expressions, string represents the original string, and flags represents flags for some special functions. This parameter can be omitted. When not omitted, it has some auxiliary functions, such as ignoring case, ignoring newline characters, and so on.

Import retext = 'beef price: ¥70, shrimp price: ¥120, broccoli price: ¥5, Pan-Fried Mackerel price: 40 ¥' prices = re.findall ('price: (. *) ¥', text) print ('price returned by regular expression: {}' .format (prices))

The result returned by findall is a list of all the matching results:

2. The function prototype of search () is:

The use of re.search (pattern, string, flags=0) is the same as that of findall (), but search () only returns the first string that meets the requirement. It is especially useful to find only the first data from super-large text, which can greatly improve the running efficiency of the program.

Import retext = 'beef price: ¥70, shrimp price: ¥120, broccoli price: ¥5, Pan-Fried Mackerel price: 40 ¥' prices = re.search ('price: (. *) ¥', text) print ('price returned by regular expression: {}' .format (prices.group (1)

For the result, if the match succeeds, it is the object of a regular expression; if no data is matched, it is None. If you need to get the matching result, you need to use the .group () method to get the value inside. The maximum argument of group () cannot exceed the number of parentheses in a regular expression. A parameter of 1 means to read the contents of the first parenthesis, a parameter of 2 means to read the contents of the second parenthesis, and so on.

The above is about the content of this article "Python regular expression example Analysis". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report