How can I extract only the hash digits in the quotations in various strings such as:
JavaScript
x
6
1
[file:hashes.'MD5' = '547334e75ed7d4eea2953675b07986b4']
2
3
[file:hashes.'SHA1' = '82d29b52e35e7938e7ee610c04ea9daaf5e08e90']
4
5
[file:hashes.'SHA256' = 'ff3b45ecfbbdb780b48b4c829d2b6078d8f7673d823bedbd6321699770fa3f84']
6
I need to extract the digits and insert into a table using this script:
JavaScript
1
4
1
if item['hash'][:12]=='[file:hashes': #it finds the hash string from Json dic like above lists
2
if item['hash'][22:-2] not in hash_column: #extracts the digits but only for MD5
3
insert_hash_table(item['hash'][22:-2]) #insert the hash digit
4
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 ? – e.g. 82d29b52e35e7938e7ee610c04ea9daaf5e08e90
Advertisement
Answer
You could split a string using ‘=’ as a delimiter. Something like this:
JavaScript
1
5
1
hash = "file:hashes.'MD5' = '547334e75ed7d4eea2953675b07986b4'"
2
3
result = hash.split('=')[1].strip().strip("'")
4
print(result)
5
This code gives me the result:
JavaScript
1
2
1
547334e75ed7d4eea2953675b07986b4
2