0

I am a noob in Java and i want a regex pattern to match sub strings in a sentence. I have multiple sub strings from which,

i want to test if any of the sub string is there in my sentence or not.

sub strings :  ice cream, coffee, new brand

Sentence1 : I have had ice cream this month.

Sentence2: I made the bill payment for coffee with card.

Sentence 3: He just signed with new brand this week

Regex for Substrings : "(ice cream|coffee|new brand).*"

What should be the correct regex in Java for this?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Rahul Anand
  • 523
  • 1
  • 7
  • 20
  • `"(ice cream|coffee|new brand)"` or `"\\b(?:ice cream|coffee|new brand)\\b"` will do with `Matcher#find` – Wiktor Stribiżew Jan 21 '20 at 10:07
  • Why you need regex for this ?? As per your question, your `String str = "ice cream, coffee, new brand";` Why not split on the basis of **,** `String [] splitted = str.split(",");` – Vishwa Ratna Jan 21 '20 at 10:08
  • @VishwaRatna I will use this regex in wso2sp , as it is java based. I need to provide Java based regex – Rahul Anand Jan 21 '20 at 10:10
  • @RahulAnand , no worries friend, Wiktor has already gave you the regex in comment. Hope it helps. – Vishwa Ratna Jan 21 '20 at 10:15

0 Answers0