In Python, how can I iterate over all regular expression matches in a string?

I want to do something (more than just substitution) with substrings matching the pattern in a longer string. If the assignment was an expression returning a value, as in C and most other programming languages, this would be (using C syntax with Python semantics):

while ( match = re.search( pat, str ) ) { /* do something to the string, using the match object, in addition to removing or replacing the substring */ } 

or in more detail, avoiding the use of assignment as an expression:

 for ( match = re.search( pat, str ); match; match = re.search( pat, str ) ) { /* do something to the string, using the match object */ } 

At least one of them is possible in most programming languages: C, C ++, Java, Perl, Javascript, ... but none of them is possible in Python. Is there a pythonic equivalent (non-messy kludgey with break or continue statement)?

+5
source share
1 answer

Perhaps you are looking for finditer :

Returns an iterator giving matching objects for all matches of matches for the RE pattern in a string. The string is scanned from left to right, and matches are returned in the order found. Empty matches are included in the result if they do not relate to the start of another match.

 #!/usr/bin/env python3 import re s = "abcabcabc"; it = re.finditer("(\w)", s) for m in it: print(m.groups()) 
  $ ./t.py ('a',) ('b',) ('c',) ('a',) ('b',) ('c',) ('a',) ('b',) ('c',) 
+4
source

Source: https://habr.com/ru/post/1270098/


All Articles