I want to match for any string of characters between two words ("Hello" and "Goodbye" in the following examples) using a regex.
The bolded areas in the following list should match:
- Hello, I like you. Goodbye.
- Hello there, do you enjoy golf?
- I like you. Goodbye. See you later.
Examples of strings that should not match at all include (basically I want to treat the words "Hello" and "Goodbye" as a kind of barrier):
- HelloGoodbye
- Goodbye, how are you?
- How are you? Hello
I tried using (?<=Hello).*(?=Goodbye), which works in some cases (see here). The issue with this regex is that if for example "Goodbye" isn't present, none of the text after "Hello" matches (and vice versa).
I'm not exactly sure that the regex I have tried is a good way to go about it. Possibly, I just need to match any part of the string that follows "Hello" and/or preceeds "Goodbye" (but neither need to be present for a match).
I believe I need to have some kind of conditional, and I guess matching the first two is easy but I am unable to find a way to do it.
Any help would be appreciated as I am still new to using regular expressions.