-1

We have keys between curly brackets and want to search these keys in some string. But when we split the string like .split(" ") some punctuation stays with the string elements. We tried the "\\p{P}" regex but it deleted the curly brackets that we need. So how do we fix this issue?

Many thanks.

String keys =  " Dear {User_Name}, your process..."
List<String> bodyContent = (Arrays.asList(keys.replaceAll("\\p{P}", "").split(" ")));

After these bodyContent = {"Dear" , "User_Name" , ...}

But we want "{User_Name}" instead of "User_Name" or "{User_Name},"

lisa p.
  • 2,138
  • 21
  • 36
Mick
  • 1
  • 2

1 Answers1

-1

To match all words that might also include any curly brackets you can use the following regex, which:

  1. {*: matches zero or more lefthand curly brackets.
  2. \w*: matches zero or more word characters.
  3. }*: matches zero or more righthand curly brackets.
({*\w*}*)

I'd suggest using regex101.com to test or make any changes you may see fit. I also have a link for the regex101 I used here.

bragi
  • 183
  • 2
  • 12