Source From Here
Question
In a program I'm writing I have Python use the re.search() function to find matches in a block of text and print the results. However, the program exits once it finds the first match in the block of text. How do I do this repeatedly where the program doesn't stop until ALL matches have been found? Is there a separate function to do this?
How-To
Use re.findall or re.finditer instead:
One simple example:
Output:
Question
In a program I'm writing I have Python use the re.search() function to find matches in a block of text and print the results. However, the program exits once it finds the first match in the block of text. How do I do this repeatedly where the program doesn't stop until ALL matches have been found? Is there a separate function to do this?
How-To
Use re.findall or re.finditer instead:
One simple example:
- import re
- text = r'''My name is John. His name is Peter.
- Today is sunny and we all want to go outside for picnic.'''
- for e in re.findall('is ([a-zA-Z]+)', text):
- print('Found "{}"'.format(e))
沒有留言:
張貼留言