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

How to extract numbers from a string by using regular expressions in python

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

Share

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

This article will explain in detail how to use regular expressions to extract numbers from strings in python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Use regular expressions, as follows:

# # Summary # # ^ matches the beginning of the string. # # $matches the end of the string. # #\ b matches the boundary of a word. # #\ d match any number. # #\ D matches any non-numeric character. # # x? Matches an optional x character (in other words, it matches the x character once or 0 times). # # x* matches 0 or more x characters. # # x + matches the x character one or more times. # # x {n < m} matches x characters, at least n times and m times at most. # # (a | b | c) either match a, match b, or match c # (x) generally represents a memory group (remembered group). You can use the re.search function to return the object's groups () function to get its value. # # A period in a regular expression usually means "match any single character"

Ideas for solving the problem:

Since the number is extracted, then the form of the number is generally: integer, decimal, integer plus decimal

So it's generally shaped like: -.

According to the meaning of the above regular expression, the following expression can be written: "\ d +\.?\ d *"

\ d + match one or more numbers, be careful not to write as * here, because even if it is a decimal point, there must be a number before the decimal point. This matches the decimal point, there may or may not be;\ d * this matches the number after the decimal point, so it is 0 or more

The code is as follows:

Import restring= "A1.45 ~ b5 ~ ~ 6.45 ~ 8.82" print re.findall (r "\ d +\.?\ d *", string) # ['1.45','5', '6.45', '8.82']

Matches the number at the beginning of the specified string

For example, the following string:

Tensorflow:Final best valid 0 loss=0.20478513836860657 norm_loss=0.767241849151384 roc=0.8262403011322021 pr=0.39401692152023315 calibration=0.9863265752792358 rate=0.0 extract calibration=0.9863265752792358. # matches the number pattern = re.compile after "calibration=" (r'(?

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