0

how to extract word from string using either words as reference to extracted

String str = "I'm eating filet-o-fish and hashed potatoes for breakfast";

between "eating"&"for breakfast" how to extract "filet-o-fish and hashed potatoes"; جزاكم خيرا よろしくお願いします。

2
  • Please only use English on Stack Overflow. Commented Jan 8, 2021 at 10:41
  • Hi, as you have now answers now, you may think about accepting an answer to reward the one that gives you the most helpful comment. Commented Feb 13, 2021 at 9:47

1 Answer 1

1

Use a Pattern and Matcher along with regex eating\\s*(.*)\\s*for breakfast, which is a capturing group between the 2 keywords

String str = "I'm eating filet-o-fish and hashed potatoes for breakfast";
Matcher m = Pattern.compile("eating\\s*(.*)\\s*for breakfast").matcher(str);

if (m.find()) {
    String inside = m.group(1);
    System.out.println(inside); // filet-o-fish and hashed potatoes
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.