0

I have the following string :

String xmlnode = "<firstname id="{$person.id}"> {$person.firstname} </firstname>";

How can I write a regex to extract the data inside the {$STRING_I_WANT}

The part I need is without {$} how can I achieve that?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Roy Bean
  • 75
  • 2
  • 8
  • `[\{](.*?)[\}]` - Full match includes brackets, but if you want the data inside then use the group match – kbz Nov 07 '17 at 18:49
  • Possible duplicate of [How to extract a substring using regex](https://stackoverflow.com/questions/4662215/how-to-extract-a-substring-using-regex) – Bernhard Barker Nov 07 '17 at 18:56

1 Answers1

0

You can use this regex \{\$(.*?)\} with pattern like this :

String xmlnode = "<firstname id=\"{$person.id}\"> {$person.firstname} </firstname>";

Pattern pattern = Pattern.compile("\\{\\$(.*?)\\}");
Matcher matcher = pattern.matcher(xmlnode);

while (matcher.find()) {
    System.out.println(matcher.group(1));
}

Note : you have to escape each character { $ } with \ because each one is special character in regex.

Outputs

person.id
person.firstname
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140