开发者

How can I find all matches to a regular expression in Python?

开发者 https://www.devze.com 2023-02-04 16:09 出处:网络
In a program I\'m writing I have Python use the re.search() funct开发者_JS百科ion to find matches in a block of text and print the results. However, the program exits once it finds the first match in

In a program I'm writing I have Python use the re.search() funct开发者_JS百科ion 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?


Use re.findall or re.finditer instead.

re.findall(pattern, string) returns a list of matching strings.

re.finditer(pattern, string) returns an iterator over MatchObject objects.

Example:

re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')
# Output: ['cats', 'dogs']

[x.group() for x in re.finditer( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')]
# Output: ['all cats are', 'all dogs are']
0

精彩评论

暂无评论...
验证码 换一张
取 消