[Solved] Checking for elements in list


The Main approach for checking elements of a list in a string is :

s=""'<a href="https://ertfwetwer" target="_blank">[Nerve Center]</a>'''
my_list=['href','a']

   def checker(mylist, my_string)
     new = list()

     for i in mylist:
        if i in my_string: # if elements is in string (you can check only special elements )
            print i ,'is in string'
            new.append(i) #storing result to new list 

   checker (my_list, s)

output :

href is in string
a is in string

But because you say I have long string from page source, and I want to see if .jpg or .png or .swf or .wbm or…… is inside if it is I want to store it in some var as str

So you want use regex to your code for finding all .jpg or more foramtting in your long string ! let’s assume you have

s=""'<a href="https://ertfwetwer" target="_blank">[Nerve Center]   
myname.jpg another.pdf mynext.xvf </a>'''

so you want to check .jpg and another formated

my_list=['.jpg','.pdf']

for i in my_list:
 if i in s:
  print i ,'is in string'

You can also find their name :

import re
s=""'<a next.pdf href="https://ertfwetwer" target="_blank">[Nerve Center] myfile.jpg another.pdf </a>'''

 re.findall(r'([^\s]+).jpg|([^\s]+).pdf',s)

output:

[('myfile', ''), ('', 'another')]

Or even

for i in  re.findall(r'([^\s]+).jpg|([^\s]+).pdf',s):
    for j in i:
        print j.strip(' ')



next
myfile
another

0

solved Checking for elements in list