Skip to content Skip to sidebar Skip to footer

Find Strings And Subtring From The Wordlist

i have test.txt file, Find strings and subtring from the wordlist

Solution 1:

have you had to use regexp? if it doesn't matter, do you want results like this?

with open('test.txt', 'r')as f:
    s = f.read()
s = s.split('\n')
s

Out[1]:
['<aardwolf>',
 '<Aargau>',
 '<Aaronic>',
 '<aac>',
 '<akac>',
 '<abaca>',
 '<abactinal>',
 '<abacus>  ']

for list-type result:

ARGVs = ['aard', 'onic', 'abacu']

matches = [x for x in s for arg in ARGVs if arg.lower() in x.lower()]
print(matches)

Out[2]:
['<aardwolf>', '<Aaronic>', '<abacus>  ']

for dict-type result

ARGVs = ['aard', 'onic', 'abacu', 'aaro', 'ac']

{key:[x for x in s if key in x] for key in ARGVs if len([x for x in s if key in x]) != 0}

Out[3]:

{'aard': ['<aardwolf>'],
 'onic': ['<Aaronic>'],
 'abacu': ['<abacus>  '],
 'ac': ['<aac>', '<akac>', '<abaca>', '<abactinal>', '<abacus>  ']}

With RegExp

import re

with open('test.txt', 'r')as f:
    s = f.read()

ARGVs = ['wol','ac']
cond = '|'.join([f'\w*{patt}\w*' for patt in ARGVs])
re.findall(cond,s)  

Out[4]:
['aardwolf', 'aac', 'akac', 'abaca', 'abactinal', 'abacus']

Post a Comment for "Find Strings And Subtring From The Wordlist"