Capture words in Ultisnips

For quick input, I’d like to expand a shorthand for loop a.fori to the whole for loop for(int i=0; i<a; i++). This could be accomplished by snippet's regex option.  The regex feature allows us to capture the trigger text by python regex.  After word capture, python interpolation !p can be used to process the groups.

# global regex group function
global !p

def group(a):
	snip.rv=match.group(a)
	return

endglobal

snippet "(\\w+).for(\\w+)" "for loop" r
for (int `!p group(2)`=0 ; i<`!p group(1)`; i++) {
	$1
}
endsnippet

snippet "(\\w+).sout" "expand sout" r
System.out.println(`!p group(1)`);
endsnippet

As presented in above code, I defined a global regex group function and two snippets. One is for loop expansion, the other is sout expansion.  The outcome is shown below.

forloops & sout