两个不错的正则
从评论中匹配 硬件 信息
使用时,请把 r""" text “”" 中的 空格删掉
我只是为了看着清晰 才将正则表达式进行了 展开
p = re.compile(r""" ( (显卡ID[((]数量[))]:\s*(?P<gpu_info>[0-9a-zA-Z:(),]+))| (内存总数:\s*(?P<memory_GB>[0-9]+)\s*GB)| (cpu线程数:\s*(?P<cpu_type>[0-9a-zA-Z]+)\s*(?P<cpu_count>[0-9]+))| (.) )+""" ,re.DOTALL)
从 conf 中匹配 硬件 信息
p = re.compile(r""" ( (hostid\s*=\s*(?P<hostid>[0-9a-zA-Z]{30,}))| (ip\s*=\s*(?P<ip>[0-9a-zA-Z:.]+))| (video_id\s*=\s*(?P<gpu_info>[0-9a-zA-Z:()(),]+))| (cpu\s*=\s*(?P<cpu_type>[0-9a-zA-Z]+)\s*(?P<cpu_count>[0-9]+))| (time\s*=\s*(?P<time>[0-9-/.]+))| (memory\s*=\s*(?P<memory_GB>[0-9]+)\s*GB)| (machine_code\s*=\s*(?P<machine_code>[0-9a-zA-Z]{30,}))| (.) )+""" ,re.DOTALL)
re group使用
import re def test(): # gpu_type gpu_count memory_GB cpu_type cpu_count in_s = " 显卡ID(数量):10de:1f82(1) 内存总数:128 GB cpu线程数:AMD 64 " # patten1 = re.compile("""显卡ID\(数量\):(?P<gpuid>[0-9a-zA-Z:() ]+)""") patten1 = re.compile("""显卡ID\(数量\):[ ]*(?P<gpu_type>[0-9a-zA-Z:]+)\((?P<gpu_count>[0-9]+)\)[ ]*内存总数:[ ]*(?P<memory_GB>[0-9]+)[ ]*GB[ ]*cpu线程数:[ ]*(?P<cpu_type>[0-9a-zA-Z]+)[ ]*(?P<cpu_count>[0-9]+)""") res = patten1.search(in_s) print(res) print("gpu_type",res.group('gpu_type')) print("gpu_count",res.group('gpu_count')) print("memory_GB",res.group('memory_GB')) print("cpu_type",res.group('cpu_type')) print("cpu_count",res.group('cpu_count')) print("\nres groups ", res.groups() ) print("\nres groupdict ", res.groupdict() ) print("\nfindall\n") res = patten1.findall(in_s) print(res) if __name__ == "__main__": test()
oputput
<_sre.SRE_Match object; span=(1, 48), match='显卡ID(数量):10de:1f82(1) 内存总数:128 GB cpu线程数:AMD 64'> gpu_type 10de:1f82 gpu_count 1 memory_GB 128 cpu_type AMD cpu_count 64 res groups ('10de:1f82', '1', '128', 'AMD', '64') res groupdict {'gpu_type': '10de:1f82', 'gpu_count': '1', 'memory_GB': '128', 'cpu_type': 'AMD', 'cpu_count': '64'} findall [('10de:1f82', '1', '128', 'AMD', '64')]
re.group 使用
patten1 = re.compile("""显卡ID\(数量\):(?P<gpuid>[0-9a-zA-Z:() ]+)内存总数:(?P<memoryinfo>[0-9a-zA-Z:() ]+)cpu线程数:(?P<cpuinfo>[0-9a-zA-Z:() ]+)""") res = patten1.search(in_s) print(res) print("gpu_type",res.group('gpuid'))
output
lmk@DESKTOP-1VJBA5P:~/codes/py/study_re$ python3 qoute.py <_sre.SRE_Match object; span=(1, 49), match='显卡ID(数量):10de:1f82(1) 内存总数:128 GB cpu线程数:AMD 64 '> gpuid 10de:1f82(1) memoryinfo 128 GB cpuinfo AMD 64 findall [('10de:1f82(1) ', '128 GB ', 'AMD 64 ')]
findall使用
res = patten1.findall(in_s) print(res)
正则匹配 文本 特定内容
import re def test(): # gpu_type gpu_count memory_GB cpu_type cpu_count in_s = " 显卡ID(数量):10de:1f82(1) 内存总数:128 GB cpu线程数:AMD 64 " # patten1 = re.compile("""显卡ID\(数量\):(?P<gpuid>[0-9a-zA-Z:() ]+)""") patten1 = re.compile("""显卡ID\(数量\):(?P<gpuid>[0-9a-zA-Z:() ]+)内存总数:(?P<memoryinfo>[0-9a-zA-Z:() ]+)cpu线程数:(?P<cpuinfo>[0-9a-zA-Z:() ]+)""") res = patten1.search(in_s) print(res) print("gpu_type",res.group('gpuid')) print("gpu_count",res.group('gpuid')) print("memory_GB",res.group('memoryinfo')) print("cpu_type",res.group('cpuinfo')) print("cpu_type",res.group('cpuinfo')) print("\nfindall\n") res = patten1.findall(in_s) print(res) if __name__ == "__main__": test()