Python re.findall
时间: 2023-11-05 10:09:18 浏览: 106
The `re.findall()` function is a method in Python's built-in `re` module used to search for all non-overlapping occurrences of a regular expression pattern in a given string and return a list of all matched substrings.
The syntax of `re.findall()` is as follows:
```python
re.findall(pattern, string, flags=0)
```
Here, `pattern` is the regular expression pattern to search for, `string` is the input string to search in, and `flags` (optional) is an integer value that can modify the behavior of the regular expression pattern.
The `re.findall()` function returns a list containing all the matched substrings in the order they were found in the input string. If there are no matches, an empty list is returned.
Example usage:
```python
import re
text = "The quick brown fox jumps over the lazy dog"
matches = re.findall(r"\b\w{3}\b", text)
print(matches)
# Output: ['The', 'fox', 'the', 'dog']
```
In this example, the regular expression pattern `\b\w{3}\b` searches for all three-letter words in the input string `text`. The `re.findall()` function returns a list of all matched substrings, which are the words "The", "fox", "the", and "dog".
阅读全文