我想在“字符串”列表的每个字符串中找到所有独立的两位数字。 是否有比我使用的正则表达式更简单的正则表达式语法来解决此问题?
import re
strings = ['21', # wanted matches: '21'
'Im 21', # wanted matches: '21'
'21 Im', # wanted matches: '21'
'34 56 78 123', # wanted matches: '34', '56', '78'
'Im21 21Im :21'] # wanted matches: no matches
match_list = []
for str in strings:
match_list.append(re.findall(r'^\d{2}$|(?<=\s)\d{2}(?=\s)|(?<=\s)\d{2}$|^\d{2}(?=\s)', str))
print(match_list)
我想到了
re.findall(r'(?<=^|\s)\d{2}(?=\s|$)', str)
我知道这个较短的表达式不起作用,但是希望您能有所了解。