How to find a word by using regex?
import re
s = 'ddxx/CS12-CS13/C512/2ABC', "sss"
for i in s:
c = re.findall('/C', i)
print(c)
I want to print elements which contain /C.
Expected output:
ddxx/CS12-CS13/C512/2ABC
You can do this using in without regex at all.
s = 'ddxx/CS12-CS13/C512/2ABC',"sss"
for i in s:
if '/C' in i:
print(i)
This gives your desired output of:
ddxx/CS12-CS13/C512/2ABC
Using regex:
import re
s = 'ddxx/CS12-CS13/C512/2ABC',"sss"
for i in s:
if re.search('/C', i):
print(i)
Gives the same output:
ddxx/CS12-CS13/C512/2ABC
Duplicate of: python's re: return True if regex contains in the string
sis supposed to be a list, but you're missing the[]around it? You're not going to find/Cini, so that does not make a lot of sense either. And I'm assuming you want to print all elements containing some other element?a, b = something(), something_else()works, because you can define the tuple with a comma, but you can just as easily unpack it with a comma as well.