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"; جزاكم خيرا よろしくお願いします。

azro
  • 53,056
  • 7
  • 34
  • 70
  • Please only use English on Stack Overflow. –  Jan 08 '21 at 10:41
  • Hi, as you have now answers now, you may think about [accepting an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) to reward the one that gives you the most helpful comment. – azro Feb 13 '21 at 09:47

1 Answers1

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
}
azro
  • 53,056
  • 7
  • 34
  • 70