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 does Python find all the numbers in a string

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

Share

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

Today, I would like to share with you the relevant knowledge points about how Python finds all the numbers in the string. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Findall

Finds all the substrings matched by the regular expression in the string and returns a list, if there are multiple matching patterns, a list of tuples, or an empty list if no matches are found.

Note: match and search are matched once findall matches all.

The syntax format is:

Re.findall (pattern, string, flags=0) or pattern.findall (string [, pos [, endpos]])

Parameters:

Pattern matching pattern.

String the string to be matched.

The optional parameter pos, which specifies the starting position of the string, defaults to 0.

Endpos optional parameter that specifies the end position of the string, which defaults to the length of the string.

Find all the numbers in the string:

Import re

Result1 = re.findall (Runoob 123 google 456')

Pattern = re.compile (r'\ dwells') # find numbers

Result2 = pattern.findall ('runoob 123 google 456')

Result3 = pattern.findall ('run88oob123google456', 0,10)

Print (result1)

Print (result2)

Print (result3)

Output result:

['123', '456'] ['123', '456'] ['88,'12']

Multiple matching patterns, returning a list of tuples:

Import re

Result = re.findall (r'(\ w +) = (\ d +)', 'set width=20 and height=10')

Print (result)

[('width',' 20'), ('height',' 10')] these are all the contents of the article "how to find all the numbers in a string by Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report