我希望通过匹配较长字符串中的模式的子字符串来做某事(不仅仅是替换).如果赋值是一个返回值的表达式,就像在C和大多数其他编程语言中一样,那就是(使用C语法和Python语义):
while ( match = re.search( pat, str ) ) {
/* do something to the string, using the match object,
in addition to removing or replacing the substring
*/
}
Run Code Online (Sandbox Code Playgroud)
或者更详细地说,避免使用赋值作为表达式:
for ( match = re.search( pat, str );
match;
match = re.search( pat, str ) ) {
/* do something to the string, using the match object */
}
Run Code Online (Sandbox Code Playgroud)
在大多数编程语言中,至少有一种是可能的:C,C++,Java,Perl,Javascript,......但它们似乎都不可能在Python中使用.是否存在pythonic等价物(不涉及带有中断或继续声明的kludgey混乱)?