-1

For example :

String str = "bla bla ${foo} ${foo1}";

How to get words "foo" and "foo1" ?

maybe my string is :

String str1 = "${foo2} bla bla ${foo3} bla bla";

How to get words "foo2" and "foo3" ?

  • 6
    One solution is using a regex. – Maroun Mar 28 '16 at 17:08
  • 4
    What code have you written? What does it do? Help us reproduce your problem. – nicomp Mar 28 '16 at 17:08
  • 1
    I'd myself search google for "java extract text between parenthesis". This [answer](http://stackoverflow.com/questions/24256478/pattern-to-extract-text-between-parenthesis) appears as first result. – Eng.Fouad Mar 28 '16 at 17:10
  • 1
    [`Pattern`](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html) + [`Matcher`](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html) will help. – Alex Salauyou Mar 28 '16 at 17:11
  • Thanks everyone, I found some solution. using Pattern + Matcher. – Sayamrat Keawta Mar 28 '16 at 17:22

2 Answers2

4

You can use the regex Pattern and Matcher classes. For example:

String str = "bla bla ${foo} ${foo1}";
Pattern p = Pattern.compile("\\$\\{([\\w]+)\\}");
Matcher m = p.matcher(str);
while(m.find()) {
    System.out.println(m.group(1));
}
/* Result:
foo
foo1
 */
Matthew Wright
  • 1,485
  • 1
  • 14
  • 38
2
Pattern p = Pattern.compile("\\${(.*?)\\}");
Matcher m = p.matcher(input);
while(m.find())
{
    //m.group(1) is your string. do what you want
}

this should work

DVN
  • 114
  • 1
  • 9