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 use regular expressions to match numbers and strings in Python

2025-02-27 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 match numbers and 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 some understanding of the relevant knowledge after reading this article.

Python regular expressions match numbers

Phone number:\ d {3} -\ d {8} |\ d {4} -\ d {7}

QQ number: [1-9] [0-9] {4,}

Postcode of China: [1-9]\ d {5} (?!\ d)

ID:\ d {15} |\ d {18}

Ip address:\ d+\.\ d+\.\ d+

[1-9]\ d * positive integers

-[1-9]\ d * negative integer

-? [1-9]\ d * Integer

[1-9]\ d* | 0 is a non-negative integer

-[1-9]\ d* | 0 is a non-positive integer

[1-9]\ d *.\ d * | 0.\ d * [1-9]\ dfloat $positive floating point number

-([1-9]\ d *.\ d * | 0.\ d * [1-9]\ d *) $negative floating point number

-? ([1-9]\ d *.\ d * | 0.\ d * [1-9]\ d * | 0?\ .0 + | 0) $floating point

Match the price and output the average price

Import reprice='25.34-34.55'test=re.compile (r'[1-9]\ d *\.\ d * | 0\.\ d * [1-9] | [1-9]\ d *') .findall (price) [0] test2=re.compile (rang-[1-9]\ d *\.\ d * |-0\.\ d * [1-9] |-[1-9]\ d *') .findall (price) [0] i=float (test) x=-float (test2) r = (xchimi) / 2print r

Knowledge point expansion: python regular expression to find pure numbers in a string

1. Simple practice

> import re > re.findall (r'\ dudes, 'hello 42 Iyma 32 string 30') ['42','32','30']

However, this makes it possible to recognize impure numbers in a string.

> re.findall ("hello 42 Iyma 32 str12312ing 30") ['42','32', '12312','30']

2. Identify pure numbers

If you only need numbers separated by word boundaries (spaces, periods, commas), you can use\ b

> re.findall (r'\ b\ d +\ baked, "hello 42 Isima 32 str12312ing 30") ['42,'32,'30] > re.findall (r'\ b\ d +\ str12312ing 30 "," hello,42 Isima 32 str12312ing 30 ") ['42,'32] > re.findall (r'\ b\ d +\ baked," hello,42 Isima 32 str12312ing 30 ") ['42,'32') What are the main application areas of python? 1. Cloud computing Typical application OpenStack. 2. WEB front-end development, many large websites are developed by Python. 3. Artificial intelligence applications, which are based on big data's analysis and deep learning, have essentially been unable to leave python. 4. System operation and maintenance project, the standard configuration of automatic operation and maintenance is python+Django/flask. 5. Financial management analysis, quantitative transaction, financial analysis. 6. Big data's analysis.

On how to use regular expressions in Python to match numbers and strings is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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