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

The example usage of re.findall function in python

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "instance usage of re.findall function in python". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. The findall function returns a list of regular expressions for all matching results in a string.

2. If the regular match that is not grouped is the returned regular match, the packet returns the grouped match instead of the whole regular match.

Example

Find all the substrings that match pattern (do not overlap) and put them in the list.

Import relst = re.findall ("[1-9]\ d *", "qw21313h2o58p4kjh8123jkh8435u") for x in lst: print (xQuery end= "")

# output result: 21313 1 58 4 8123 8435

Instance expansion:

Function description in python3:

Findall (pattern, string, flags=0) Return a list of all non-overlapping matches in the string. If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.

Two forms of use:

Import rekk = re.compile (r'\ dcast') kk.findall ('one1two2three3four4') # [1pje 2je 3je 4] # pay attention to the usage of findall () here, two parameters can be passed; kk = re.compile (r'\ dcast') re.findall (kk, "one123") # [1je 2je 3]

Among them, when including (), you should pay attention to:

Import restring= "abcdefg acbdgef abcdgfe cadbgfe" # difference between parentheses and no parentheses # without parentheses regex=re.compile ("((\ w+)\ s +\ w+)") print (regex.findall (string)) # output: [('abcdefg acbdgef',' abcdefg'), ('abcdgfe cadbgfe',' abcdgfe')] regex1=re.compile ("(\ w+)\ s +\ w+") print (regex1.findall (string)) # output: ['abcdefg' 'abcdgfe'] regex2=re.compile ("\ w +\ s +\ w +") print (regex2.findall (string)) # output: [' abcdefg acbdgef', 'abcdgfe cadbgfe'] "instance usage of re.findall function in python" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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