1

I have a list

a = ["CTC", "CTCglitches", "CTCremoval"]

I am running

re.search("CTC", a[0]),....,re.search("CTC", a[2]).

I only want to return True for re.search("CTC", a[0]), and False for the remaining two words.

Is there any option in regex?

Cow
  • 2,543
  • 4
  • 13
  • 25
Anirudh
  • 61
  • 6

1 Answers1

1

Below code will work for you. . We just need to use re.search("^CTC$", word)

import re

a = ["CTC", "CTCglitches", "CTCremoval", "CTC test", "testCTC", "test CTC"]
for word in a:
    match = re.search("^CTC$", word)
    if match:
        print(True)
    else:
        print(False)
Ashad0203
  • 97
  • 5