I learn Python, and bumped into interesting thing: I try to write lambda-function, which will search the pattern in the string, and return result if found matching, otherwise empty string. What I already have wrote:
>>> b = lambda x: re.search("_\w+_", x).group(0) if re.search("_\w+_", x) is not None else ''
If my string has only one match - this works ok, but I don't know how can I return dict with my results if string contain more than one match in the passed string. Example:
>>&开发者_C百科gt; b('sdsd _sdsd_ sdsd sdsd _sssssssss_')
'_sdsd_'
Or tell me please, how can I write that lambda better? UPD Oh, sorry, I forgot: I do it in Python 2.7
Don't write it at all.
>>> re.findall("_\\w+_", 'sdsd _sdsd_ sdsd sdsd _sssssssss_')
['_sdsd_', '_sssssssss_']
>>> re.findall("_\\w+_", '')
[]
精彩评论