Finding if common sub-string exists with regex

I want to find if there is a sub-string of the string "chef" but in order in another given string with length > 1.
So basically we want strings ch, he, ef, che, hef or chef to exist in the given string.

Ex:
1> kefaa
Here we have ef which is part of ''chef'' so it is a valid string.
2> fhlasek
Here we have fh which characters exist in 'chef' but the sequence is incorrect so it is invalid.

I have this code that works but here adding substrings manually is easy as the string 'chef' has quite fewer possibilities but I want a code that will work for any given string.

import re
pattern = r"(ch|he|ef|che|hef|chef)"
s = input()
res = re.search(pattern, s)
if bool(res):
    print('YES')
else:
    print('NO')

P.S. Im sorry if this question was already asked and solved, I was unable to find it.
Thank You.

Read more here: https://stackoverflow.com/questions/68489118/finding-if-common-sub-string-exists-with-regex

Content Attribution

This content was originally published by Pratik Mogaveera at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: