-3

I would like to write an expression to search for 2 terms in a sentence. One term is constant one term changes.

For example I would be searching for the word "Ben" as a constant plus the word "banana" or "apple"

"Ben likes bananas" would return positive

"Ben eats banana" would return positive

"Ben eats apples" would be positive

"Ben likes apple" would be positive

"Ben likes pears" would be negative because pears was not a specified term.

"Tom likes apple" would be negative because Ben is not specified

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 3
    Must banana or apples appear after Ben? – Sweeper Sep 12 '19 at 05:56
  • 1
    Please explain more about the pattern. Is it ""? Or can there by more after the part you showed? Can there be more than one word in between? Can there be anything before the keyword? Do you know that the word to find is always either "banana" or "apple"? Please give more input examples. Do you just want a boolean (yes matches, no match)? Or do you want to capture the word, so that you can print it afterwards? – Yunnosch Sep 12 '19 at 06:07
  • 1
    By the way, try to avoid the "I want. Please code it for me." impression. To do that, show your own attempts to achieve your goal. That would also help clarify what you actually want. Some of the information lack can be replaced by interpreting your coding attempts. – Yunnosch Sep 12 '19 at 06:10
  • 1
    Please state which kind of regex you use. I.e. name the programming language or the tool you are using as environment. – Yunnosch Sep 12 '19 at 06:14
  • Thank you for the comments so far! To clarify I am using tasker with autonotification to trigger an event when a certain person messages a group with a certain word. – Lamfeihong Sep 13 '19 at 02:55
  • I would always know the words I would like to find like banana Ben and apple. Other examples would be: Ben likes to eat apple - positive, apples like Ben - possitive, Ben likes apples and bananas - positive, Ben likes pears and apples - positive, Tom likes apple and banana - negative. I don't need to use the words. Only a Boolean return is needed. Thank you so much!! – Lamfeihong Sep 13 '19 at 03:03

1 Answers1

0

Try Ben.+(?:banana|apple)

Explanation:

Ben.+ - match Ben and one or more of any characters

(?:...) - nno-captuirng group

banana|apple - match either banana or apple

Demo

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • 1
    You did interpret the question differently than I did and have made a few assumptions which I would have guessed differently. Asking OP for clarification seems to be the more appropriate way here. On the other hand, this answer will probably provoke OP to clarify.... ;-) – Yunnosch Sep 12 '19 at 06:12
  • This actually works perfectly! Thank you so much!! – Lamfeihong Sep 13 '19 at 03:21
  • @Lamfeihong If it helped you, you should accept the answer (by clicking green check mark on the left) – Michał Turczyn Sep 13 '19 at 08:24