Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Python find all the numbers in a string

Shulou Source: shulou.com Published: 2022-06-01 01:33:26 10月24日 Update

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.

Tags: Characters strings numbers knowledge articles parameters patterns locations content multiple different large most that is more formats regularities knowledge points results industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft Shulou Technology Linux Huawei