-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" ?

5
  • 6
    One solution is using a regex. Commented Mar 28, 2016 at 17:08
  • 4
    What code have you written? What does it do? Help us reproduce your problem. Commented Mar 28, 2016 at 17:08
  • 1
    I'd myself search google for "java extract text between parenthesis". This answer appears as first result. Commented Mar 28, 2016 at 17:10
  • 1
    Pattern + Matcher will help. Commented Mar 28, 2016 at 17:11
  • Thanks everyone, I found some solution. using Pattern + Matcher. Commented Mar 28, 2016 at 17:22

2 Answers 2

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
 */
Sign up to request clarification or add additional context in comments.

Comments

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

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.