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
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
assuming your s is a list of strings,
import re
s = ['ddxx/CS12-CS13/C512/2ABC', "sss"]
for i,string in enumerate(s):
if len(re.findall('/C', string)) > 0 :
print(s[i])
will give you the desired output ddxx/CS12-CS13/C512/2ABC