python regular record

 

Table of contents

search for decimals in string

experiment

rule explanation

References


Some regexes are always searched multiple times when used, hereby record

search for decimals in string

experiment

For example the following two

1) It is expected to find 44 and 44.8 data respectively

100 packets transmitted, 56 received, 44% packet loss, time 1001ms
100 packets transmitted, 56 received, 44.8% packet loss, time 1001ms

2 ) The regularity used:

re.findall(r"(\d+\.*\d*)(?=% packet loss)", ping_result)

rule explanation

a . In order to find out the specific string in front of the pack loss, it is necessary to use the forward positive search keyword, namely:

正则表达式B(?=正则表达式A)	

Components : parentheses, question marks, equal signs and matching strings. In this example, the matching string is % packet loss

That is, after searching for the matching string % packet loss, the rest of the regular expression, in this example (\d+\.*\d*) matches the characters before the string % packet loss.

In short: use regular expression B to match the string in front of regular expression A.

b . In order to return only 44 or 44.8, the () keyword needs to be used, namely:

(正则表单时)	      对正则表达式分组并记住匹配的文本

c . Decimal

\d+\.*\d*

 Here we see that some information on the Internet is \d+\.\d+ In fact, it can only match decimals, which cannot meet this requirement

References

(6 messages) The difference and examples of the positive/negative mode of Python regular expression forward/backward search_Xiaolong's Blog in Shandong-CSDN Blog

Python Regular Expressions | Rookie Tutorial (runoob.com) 

Guess you like

Origin blog.csdn.net/proware/article/details/126188915