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

Example Analysis of python regular expression

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "python regular expression example analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "python regular expression example analysis"!

Try to calculate the four expressions of the string. Note that the ±* / of 2 numbers can be used in the code, but the expression can not be evaluated with the expression function of python, nor can it be operated with the eval function. An example of the four expressions of a string is as follows:

Compute ='10-3 * (20-10 + (- 10 + (- 10) 5) * 27 racer 3-(- 100) / (10-3)) + (- 2.5 inch / 12) 'exercise the first step, match the numbers:

Matching numbers (including positive integers, negative integers, positive floating point numbers, negative floating point numbers):

-?\ d + (\.\ d +)?

It can be tested that matches 123,-123, 1.23,-1.23 are all successful.

Expression parsing:

-? It means a match. Represents 0 or 1.

\ d + indicates matching numbers, and + indicates one or more numbers.

(\.\ d +)? This part is the most complicated and can be explained step by step. In parentheses is\. Indicates that it matches the decimal point (if you do not add\, it will match to any character except the newline character);\ d + ditto; a pair of parentheses followed by? Indicates that these contents may not appear or appear once, meaning that they can match the decimal point and the following one or more digits as well as the digits without the decimal point and the following digits.

There is no problem with the above regular expression tested by the webmaster tool website, but there will be problems in the code in python. Please see the following code example:

Num1 = 'The number is?' num2 = 'The number is-123?' num3 = 'The number is 1.23?' num4 = 'The number is-123?' dig1 = re.compile ('-?\ d + (\.\ d +)?) dig2 = re.compile ('-\ d + (?:\.\ d +)?) print (dig1.findall (num1)) print (dig1.findall (num2)) print (dig1.findall (num3)) print (dig1.findall (num4)) print Dig2.findall (num1)) print (dig2.findall (num2)) print (dig2.findall (num3)) print (dig2.findall (num4)) out: [''] ['] ['.23'] [''] ['123'] ['-123'] ['1.23'] ['-123']

If you read the code carefully, you can see that the findall function cannot extract a number using the dig1 regular expression string, while the dig1 regular expression has clearly been verified to be correct, so why can't it extract a number?

The reason for this is because of the conflict of grouping concepts! The grouping of regular expressions is represented by parentheses, and the grouping of re modules is also represented by parentheses. Please carefully understand the differences between the two grouping concepts, which is very important!

The grouping of regular expressions is to wrap a piece of expression as a whole, and then you can do operations on the whole, such as or | and the range.? * {mmagnetic n}.

The grouping of the re module means that the content matched by the regular expression is filtered again, and only the matching part of the content within the group is needed.

The solution is to add?: to the grouping of regular expressions followed by the left parenthesis to hide the grouping, telling the python interpreter that the grouping does not mean re-filtering.

Exercise step 2, match the contents in the inner parentheses

Next, try to match the contents of the inner parentheses in the four operation strings:

Compute ='10-3 * (20-10 + (- 10 + (- 10) 5) * 27 racer 3-(- 100) / (10-3) 5) + (- 5 inch Mueller 6)'

Match innermost parentheses:

\ ([\ d.girls / ±] +?\)

The test on the webmaster tools website can successfully match the following:

(- 10: 5), (- 100), (10-3: 5), (- 5: 5)

Expression parsing:

First match the parentheses outside, note that they are all escaped with\, and then observe the regularity of the target string and fill in the parentheses.

The character sets that can be used in parentheses are numbers, decimal points, addition, subtraction, multiplication and division. They can appear one or more times. Note that they must be written after quantifier +. Identified as the shortest match.

Then test the regular expression in python.

Compute ='10-3 * (20-10 + (- 10 +) / (10-3) + (- 5) result 6) 'result = re.findall ('\ ([\ d\. * / + -] +?\)', compute) print (result) out: [(- 10) 5), (- 100), (10-3)), (- 5))] exercise step 3 Write four expression codes for calculating strings

Before I wrote the code, I thought it would be done in half an hour at the most, but I was hit in the face. It took less than half an hour to write the code and more than an hour to debug! It won't be done for two hours. It's really easier looked like than done! Friends who learn from python are advised to try to do this exercise. Doing this exercise is very helpful for mastering regular expressions and exercising your programming skills.

I decided to publish the programming idea, code and debugging process in a few days. Let's post a picture of the calculation results today.

Thank you for your reading, the above is the content of "python regular expression example Analysis". After the study of this article, I believe you have a deeper understanding of the problem of python regular expression example analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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