需要帮助从列表中提取数据模式,如下所示

时间:2018-07-23 12:24:29

标签: regex python-3.x python-2.7

我有如下所示的数据示例,我想提取具有键/值模式的数据,例如:

'Signature Algorithm: ecdsa-with-SHA256', 'Not Before: Jul 23 00:00:00 2018 GMT', 'Not After : Jul 19 00:00:00 2033 GMT, 'Public Key Algorithm: id-ecPublicKeyid', Public-Key: (256 bit)' 

示例:

['Certificate:', 'Data:', 'Version: 3 (0x2)', 'Serial Number:', 'b9:d9:f5:38:f8:42:6a:f9', 'Signature Algorithm: ecdsa-with-SHA256', 'Issuer: C=US, ST=NC, L=Raleigh, O=Eaton Corporation, OU=Electrical Division, CN=PowerXpert-02-00-34-56-63-01', 'Validity', 'Not Before: Jul 23 00:00:00 2018 GMT', 'Not After : Jul 19 00:00:00 2033 GMT', 'Subject: C=US, ST=NC, L=Raleigh, O=Eaton Corporation, OU=Electrical Division, CN=PowerXpert-02-00-34-56-63-01', 'Subject Public Key Info:', 'Public Key Algorithm: id-ecPublicKey', 'Public-Key: (256 bit)', 'pub:', '04:bf:72:4b:01:8b:5c:46:98:96:a6:d6:06:7b:d8:', '73:50:2f:47:85:60:f0:38:25:79:3d:96:be:20:3b:', '3f:39:c8:58:62:e9:d7:b6:f8:3a:6b:24:50:e1:5c:', '78:ce:5e:28:f2:60:3a:b6:cc:43:0e:0b:2b:d6:03:', '51:76:21:a4:78', 'ASN1 OID: prime256v1', 'NIST CURVE: P-256', 'X509v3 extensions:', 'X509v3 Subject Key Identifier:', '6A:E0:28:A7:17:2B:65:01:FD:31:48:5C:68:24:94:4B:42:49:76:58', 'X509v3 Basic Constraints: critical', 'CA:TRUE', 'X509v3 Key Usage: critical', 'Digital Signature, Certificate Sign, CRL Sign', 'X509v3 Subject Alternative Name:', 'DNS:PXG900-56-63-01', 'Signature Algorithm: ecdsa-with-SHA256', '30:45:02:20:43:33:58:ce:ef:f7:fd:a8:60:21:15:a3:2b:35:', '8c:1f:13:a0:1e:77:05:6f:1a:bb:a0:b6:fe:f3:ea:7b:6d:31:', '02:21:00:cf:db:9a:d1:6b:88:ae:fb:d5:5c:5a:db:0a:a0:eb:', 'a9:c9:4a:52:d0:57:18:9c:58:1b:67:42:47:c5:ec:bf:b0', '', '']

在正则表达式下使用,但未获得预期的结果

regex = re.compile(r'''
[\S]+:                # a key (any word followed by a colon)
(?:
\s                    # then a space in between
(?!\S+:)\S+           # then a value (any word not followed by a colon)
)+                    # match multiple values if present
''', re.VERBOSE)

matches = regex.findall(str(lines))
print(matches)

4 个答案:

答案 0 :(得分:1)

您可以使用以下正则表达式:

kernel matrix which as stated before is the null(A), must be 10x10

  • (?='[A-Za-z]+[\s-][A-Z][a-z]+\s?:?)'[^']+:[^']+'正向前进。断言以下是:
    • (?=,撇号'[A-Za-z]+,后跟字母字符。
    • '的空白或破折号[\s-]
    • -字母字符。
    • [A-Za-z]+可选空格。
    • \s?可选冒号:?\s,后跟空白。
  • :近距离搜索。

  • )匹配撇号'[^']+字符,而不是撇号'字符的任何字符。

  • '匹配一个冒号。

  • :匹配所有非撇号[^']+'的字符,后跟撇号'

您可以实时测试正则表达式here


Python代码段:

'

输出:

import re
lines = ['Certificate:', 'Data:', 'Version: 3 (0x2)', 'Serial Number:', 'b9:d9:f5:38:f8:42:6a:f9', 'Signature Algorithm: ecdsa-with-SHA256', 'Issuer: C=US, ST=NC, L=Raleigh, O=Eaton Corporation, OU=Electrical Division, CN=PowerXpert-02-00-34-56-63-01', 'Validity', 'Not Before: Jul 23 00:00:00 2018 GMT', 'Not After : Jul 19 00:00:00 2033 GMT','Public Key Algorithm: id-ecPublicKey',
'Public-Key: (256 bit)']
matches = re.findall(r"(?='[A-Za-z]+[\s-][A-Z][a-z]+\s?:?)'[^']+:[^']+'",str(lines))
for match in matches:
    print (match)

答案 1 :(得分:0)

我不确定我是否完全理解。但是你不能走吗?

extracted = []
for item in lines:
    if len(item.split(": ")) == 2: # It has 2 items
        extracted.append(item)

这将提取每个在“:”两边带有“东西”的项目。 我希望这会有所帮助。

答案 2 :(得分:0)

您可以使用此正则表达式提取键值对。

([a-zA-Z0-9 ]*:[a-zA-Z0-9\- :]*)

Demo Link

答案 3 :(得分:0)

 re.findall("'[A-Z][^':]+:[^',:]+(?:\d{2}:){0,3}[^',:]+'(?=,)",str(lines))
Out[139]: 
["'Version: 3 (0x2)'",
 "'Signature Algorithm: ecdsa-with-SHA256'",
 "'Not Before: Jul 23 00:00:00 2018 GMT'",
 "'Not After : Jul 19 00:00:00 2033 GMT'",
 "'Public Key Algorithm: id-ecPublicKey'",
 "'Public-Key: (256 bit)'",
 "'ASN1 OID: prime256v1'",
 "'NIST CURVE: P-256'",
 "'X509v3 Basic Constraints: critical'",
 "'CA:TRUE'",
 "'X509v3 Key Usage: critical'",
 "'DNS:PXG900-56-63-01'",
 "'Signature Algorithm: ecdsa-with-SHA256'"]