-2

Looking for a regular expression to match only the given list of words in a strings

The regular expression pattern to match strings containing only boy or apple in the below list of strings

[apple, boy, apple, boy] 
[boy, boy, boy, boy] 
[apple, boy, boy, boy] 

to match

[apple, boy, apple, boy]
[apple, boy, boy, boy] 
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
karthikbharadwaj
  • 368
  • 1
  • 7
  • 17
  • 1
    See [Check list of words in another string](http://stackoverflow.com/questions/3271478/check-list-of-words-in-another-string). – Wiktor Stribiżew Nov 17 '16 at 16:10
  • For future reference, and especially if you haven't actually done anything, just state your problem and let people that know better decide what tool works best. Regex is not the right tool for this job. – Mad Physicist Nov 17 '16 at 16:12
  • I understand this could be achieved in python in multiple ways, but I am looking for a pure regular expression approach. – karthikbharadwaj Nov 17 '16 at 16:13
  • You should clarify what language you are working with, as different languages have varying constructs and options for regular expressions. It sounds like you need one that allows you to match a previously matched pattern during its search. The structure of your data is not clear, either - your question could be interpreted to mean none of the strings you provided will match. – Suncat2000 Nov 17 '16 at 16:20
  • @karthikbharadwaj I hope you meant that the string should contain both apple **and** boy. And not apple **or** boy. – Mohammad Yusuf Nov 17 '16 at 16:40

1 Answers1

1

Try this if it works:

(?=.*apple)(?=.*boy).*
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78