如何从optparse中获取用户的输入?

时间:2018-02-12 23:13:04

标签: python regex

我有以下文件:

System Configuration Utility Version 14.1 Build 19
Copyright (c) 2017 Intel Corporation

System BIOS and FW Versions:
BIOS Version............. SE3.4

ME Firmware Version...... 2.01

BMC Version
 Op Code................. 1.2.7Q

SDR Version.............. SDR Package 0.1

我想抓住它并存储在dict中:

d ={'BMC Version': '1.2.7Q',
    'SDR Version': '0.1',
    'ME Firmware Version': '2.01'
   }

如何使用regex和python进行操作?

现在我的方法是硬编码,但我不想硬编码:

SDR Package \d+.\d+

1 个答案:

答案 0 :(得分:1)

正则表达式((?:ME Firmware|BMC|SDR) Version)\D+([0-9.A-Za-z]+)

详细说明:

  • <TextView android:id="@+id/person_active_list_name" android:paddingLeft="@dimen/person_list_name_left_padding" android:layout_width="wrap_content" android:layout_height="@dimen/person_list_image_size" android:layout_toEndOf="@id/person_active_thumbnail" android:layout_toLeftOf="@id/person_active_list_arrow" android:layout_toRightOf="@id/person_active_thumbnail" android:layout_toStartOf="@id/person_active_list_arrow" android:gravity="center|start" android:textColor="@drawable/list_item" /> 非捕获组
  • (?:)
  • |匹配列表中的单个字符
  • []匹配一次且无限次

Python代码

+

输出:

import re

d = {}
for match in re.findall(r'((?:ME Firmware|BMC|SDR) Version)\D+([0-9.A-Z]+)', text):
    d[match[0]] = match[1]

Code demo