-1

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
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 2
    One of the greatest challenges that beginners of any new thing face is learning the terminology. That makes it difficult to find the questions that have already been asked. In recognition of that, here is a question that should help you: https://stackoverflow.com/q/15374969/2988730 – Mad Physicist Apr 03 '19 at 06:05
  • Whoa, easy on the formatting there, Picasso - I'm assuming `s` is supposed to be a list, but you're missing the `[]` around it? You're not going to find `/C` in `i`, so that does not make a lot of sense either. And I'm assuming you want to print *all* elements containing some other element? – Grismar Apr 03 '19 at 06:07
  • but i want to use with regex(regular expression) – python_learner Apr 03 '19 at 06:07
  • 4
    @Grismar. All it takes is a comma to make a tuple – Mad Physicist Apr 03 '19 at 06:08
  • @python_learner. Why do you want to use regex? – Mad Physicist Apr 03 '19 at 06:12
  • 2
    Here is an exact dupe: https://stackoverflow.com/q/9012008/2988730 – Mad Physicist Apr 03 '19 at 06:14
  • @Grismar It appears the student is the Master here, no? – FailSafe Apr 03 '19 at 06:21
  • @FailSafe not sure I'm following? Of course it's perfectly fine for `s` to be a tuple, but given the state of the code, it didn't seem obvious that OP knew that was what they wanted - a `list` as a data type seems to make more sense in this instance. What exactly did you want to add here? – Grismar Apr 03 '19 at 07:22
  • @Grismar I didn't want to add anything. When I firs saw and tested it I was shocked. I had no clue Python accepted such notation, even Python2. I thought you ran into the same fun-fact I did. – FailSafe Apr 03 '19 at 07:57
  • 1
    @FailSafe that's cool - note that it works on both sides, this was a really cool thing I liked about Python when I found out: `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. – Grismar Apr 04 '19 at 00:25

2 Answers2

2

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

Salvatore
  • 10,815
  • 4
  • 31
  • 69
1

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

Shijith
  • 4,602
  • 2
  • 20
  • 34