import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.List;
public class CurlyBracketMatcher {
public static void main(String[] args) {
String input = "This is a {string} with {multiple} {words} wrapped in curly brackets.";
List<String> matches = extractWordsInCurlyBrackets(input);
System.out.println("Matches: " + matches);
System.out.println("Total Count: " + matches.size());
}
public static List<String> extractWordsInCurlyBrackets(String input) {
// Define the RegEx pattern to match text in curly brackets
String regex = "\\{([^}]+)\\}";
// Compile the pattern
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
// List to store the matches
List<String> matches = new ArrayList<>();
// Find matches and add the captured group to the list
while (matcher.find()) {
matches.add(matcher.group(1)); // group(1) contains the text inside the brackets
}
return matches;
}
}
/*
run:
Matches: [string, multiple, words]
Total Count: 3
*/