Python: Standard Library - String regular match

The re module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:

import re
re.findall(r’\bf[a-z]*’, ‘which foot or hand fell fastest’)
[‘foot’, ‘fell’, ‘fastest’]

re.sub(r’(\b[a-z]+) \1’, r’\1’, ‘cat in the the hat’)
‘cat in the hat’

If you only need a simple function, you should first consider the string method, because they are very simple, easy to read and debug:

‘tea for too’.replace(‘too’, ‘two’)
‘tea for two’

Guess you like

Origin blog.csdn.net/weixin_44523387/article/details/92162989