Get the App
SLTechnology News&Howtos  ›  Development  › 

The example usage of re.findall function in python

Shulou Source: shulou.com Published: 2022-06-03 20:15:43 09月22日 Update

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!

Tags: Output function instance regular parenthesis grouping content more knowledge result utility achievement next two usage parameter dilemma character string actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi Shulou Information Huawei Linux