regex - Extract substring using python re.match -


i have string as

sg_ts_feature_name_01_some_xyz 

in this, want extract 2 words comes after pattern - sg_ts underscore seperation between them

it must be,

feature_name 

this regex,

st = 'sg_ts_my_feature_01' = re.match('sg_ts_([a-za-z_]*)_*', st) print a.group() 

returns,

sg_ts_my_feature_ 

whereas, expect,

my_feature 

the problem asking whole match, not capture group. the manual:

group([group1, ...]) returns 1 or more subgroups of match. if there single argument, result single string; if there multiple arguments, result tuple 1 item per argument. without arguments, group1 defaults 0 (the whole match returned). if groupn argument zero, corresponding return value entire matching string; if in inclusive range [1..99], string matching corresponding parenthesized group.

and asked a.group() equivalent a.group(0) whole match. asking a.group(1) give capture group in parentheses.


Comments