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

What are the skills of python regular expressions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what are the skills of python regular expressions". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Minimum matching

Format:

Quantifier?

Note: the quantifiers are {nMagol m},?, +, *

Use:

Find the shortest substring in a string that meets the regular expression rules.

Case study:

The existing string num = "10-3 * (20-10 + (- 10 + (- 10-5)) 27-max) 3-(- 100) / (10-35)", to extract the contents in parentheses.

Import renum ='10-3 * (20-10 + (- 10 +) / (10-3) + (- 5) num2 6) 'num2 = re.findall ('\ (. +?\), num) num3 = re.findall ('\ (. +\)', num) print (num2) print (num3) out: ['(20-10 + (- 10 + 5)','(- 100)' '(10-3-5)'] [(20-10 + (- 10-10) 5) * 27-3) 3-(- 100) / (10-3-5))']

In the above code, num2 uses the minimum match, while num3 uses the default maximum match.

Notice the regular expression of num2:

. Indicates that all characters except newline characters are matched

+ represents one or more

? Represents the minimum match, followed by the first match), which returns the result. Without this? Will match to the last) before the result is returned.

Grouping

Format:

(\ w)

Note: a pair of parentheses in a regular expression indicates a grouping. There can be multiple groupings in a regular expression.

Use:

Match the string, only extract the content in the group, and discard the content of the non-group and do not extract it.

Case study:

Str1 = 'pythonjavajavascript'''result1 = re.findall (' (\ w+)', str1) print (result1) result2 = re.findall ('\ wicked mother mother str1) print (result2) out: ['python',' java', 'javascript'] [' await, 'baked,' c']

You can see the magic of grouping from the differences in extracting content between result1 and result2, which can specify that content that conforms to a regular expression is extracted.

Group hiding

Format:

(: regular expression)

Note: if the grouping begins with?: in parentheses, the matching content will be hidden.

Also: group hiding and group naming cannot be used at the same time.

Use:

When the useful data is mixed with the useless data, it can only be taken out first, then the useless data can be grouped and hidden, and the regular expressions corresponding to the useful data can be wrapped in (), so the useful data can be extracted.

Case study:

Result1 = re.findall ('1 (\ d) (\ d)', '167189') print (result1) result2 = re.findall (' 1 (?:\ d) (\ d), '167189') print (result2) out: [(' 613,'7'), ('84th,' 9')] ['7','9'] group naming

Format:

(P regular expression)

Note: P, the title number of the English book is the name of the group.

Use:

Group naming names different groups when there are many segments, which is easy to use and prevents confusion.

Case study:

Str1 = 'study python every day'ret = re.search (' (?), str1) print (ret) print (ret.group ('tag')) print (ret.group (' name')) out:tagstudy python every day

The above case is a simple demonstration, with only two groups. But the actual crawler will often encounter more than 10 groups. It is easy to confuse the data when there are a large number of groups. it is very convenient to use grouping naming to read the corresponding data according to the name, which improves the readability of the code.

That's all for "what are the skills of python regular expressions?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report