I have a problem where I need to find if a word(for example "output") is present in a snake_case word.
Which means the regex must be capable of matching all of the following situations-
- output
- output_of_my_program
- my_output_from_program
- program_output
i.e. output, output_, output, and _output need to be matched
currently I have three individual regex patterns to cover all the cases, which are-
"^output[^a-z]_?""_output_""_output$"
I however have tried to combine the three into one, which is [a-z]*_?[^a-z]output_?[a-z]* but this fails for certain cases. Is it possible to combine the three patterns into one in this case?
Edit:The other keywords I am interested in are "in", "input", "out" and the challenge is to avoid matches with words such as "introspection" and other cases such as "my_programoutput"
>Solution :
You don’t include it in your test cases, but I assume part of the intent is to explicitly not match a string like my_floutputs_from_program. You could do this with a tricky regex, but I’d just use split and in:
for s in (
'output',
'output_of_my_program',
'my_output_from_program',
'program_output',
'input',
'my_floutputs_from_program'
):
print(f"{s}: {'output' in s.split('_')}")
output: True
output_of_my_program: True
my_output_from_program: True
program_output: True
input: False
my_floutputs_from_program: False