import re str = '''/begin MEASUREMENT 100 LINK DISPLAY SYMBOL /end MEASUREMENT''' regex = r'/begin MEASUREMENT([\s\S]*)/end MEASUREMENT' matches = re.findall(regex, str) for match in matches: print(match)
1 2 3 4 5
import re str = 'test:100 end' regex = r'test:([\s\S]*)/end' matches = re.findall(regex, str) test = matches[0].strip()
统计字符串中某个单词的出现的次数
1 2 3 4 5 6
a = 'test 123 dfg test' ## 方法1 len([i for i in a.split(' ') if i == test])
string = " hello , world !" string = [x.strip() for x in string.split(',')]
将格式化字符转换为字典
1 2 3
string = "dst='192.168.0.1',src='192.168.1.2'" fields = dict((field.split('=') for field in string.split(','))) fields = dict(((lambda a:(a[0].strip("'"),a[1].strip("'"))) (field.split('=')) for field in string.split(',')))