简体   繁体   English

从字符串中提取 hash 位数字

[英]extract hash digits from string

How can I extract only the hash digits in the quotations in various strings such as:如何仅提取各种字符串中引号中的 hash 位,例如:

[file:hashes.'MD5' = '547334e75ed7d4eea2953675b07986b4']

[file:hashes.'SHA1' = '82d29b52e35e7938e7ee610c04ea9daaf5e08e90']

[file:hashes.'SHA256' = 'ff3b45ecfbbdb780b48b4c829d2b6078d8f7673d823bedbd6321699770fa3f84']

I need to extract the digits and insert into a table using this script:我需要提取数字并使用此脚本插入到表中:

if item['hash'][:12]=='[file:hashes':    #it finds the hash string from Json dic like above lists
     if item['hash'][22:-2] not in hash_column:    #extracts the digits but only for MD5
          insert_hash_table(item['hash'][22:-2])   #insert the hash digit

So in the above example if the strings before the '=' changes due to different hashes then I won't be able to be consistent with my piece of code.因此,在上面的示例中,如果“=”之前的字符串由于不同的哈希值而发生变化,那么我将无法与我的代码保持一致。 Is there anyway to extract only the digits after '=' inside the quotations for all type of hashes?无论如何只提取所有类型哈希的引号内'='之后的数字? - eg 82d29b52e35e7938e7ee610c04ea9daaf5e08e90 - 例如82d29b52e35e7938e7ee610c04ea9daaf5e08e90

Try ( regex101 ):尝试( regex101 ):

import re

s = """\
[file:hashes.'MD5' = '547334e75ed7d4eea2953675b07986b4']
[file:hashes.'SHA1' = '82d29b52e35e7938e7ee610c04ea9daaf5e08e90']
[file:hashes.'SHA256' = 'ff3b45ecfbbdb780b48b4c829d2b6078d8f7673d823bedbd6321699770fa3f84']"""

pat = re.compile(r"=\s*'([^']+)'")

for m in pat.findall(s):
    print(m)

Prints:印刷:

547334e75ed7d4eea2953675b07986b4
82d29b52e35e7938e7ee610c04ea9daaf5e08e90
ff3b45ecfbbdb780b48b4c829d2b6078d8f7673d823bedbd6321699770fa3f84

You could split a string using '=' as a delimiter.您可以使用“=”作为分隔符来拆分字符串。 Something like this:像这样:

hash = "file:hashes.'MD5' = '547334e75ed7d4eea2953675b07986b4'"

result = hash.split('=')[1].strip().strip("'")
print(result)

This code gives me the result:这段代码给了我结果:

547334e75ed7d4eea2953675b07986b4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM