我希望对长字符串中匹配某个模式的子字符串进行处理(不仅仅是替换)。如果赋值操作是一个返回值的表达式,就像C语言和大多数其他编程语言一样,那么可以这样做(使用Python语义的C语法):
while ( match = re.search( pat, str ) ) {
/* do something to the string, using the match object,
in addition to removing or replacing the substring
*/
}
更详细地说,避免将赋值作为表达式使用:
for ( match = re.search( pat, str );
match;
match = re.search( pat, str ) ) {
/* do something to the string, using the match object */
}
在大多数编程语言中,至少有一种方式可以实现以下操作:C、C++、Java、Perl、Javascript等等,但是在Python中似乎没有这样的方法。是否有一种Pythonic的等效方法(不涉及使用break或continue语句的混乱解决方案)?