How to extract words in braces using regular expressions?

I want to extract all the words enclosed in braces, so I have expressions such as

foo {bar} moo {mar}

The matching string can have any number of these words, but I'm starting to think that I am approaching this problem incorrectly.

My attempt

And I tried to extract the bracket words into groups so that I could use every single match. So, I made a regex:

String rx = ".*\\{({GROUP}\\w+)\\}.*";

Note. I use JRegex syntax, so I need to get away from some curls.

Result

- ( ) bar, bar mar. ? , , - ., , .

!

+3
2

.*\{({GROUP}\w+)\}.* , :

  • .* foo
  • \{({GROUP}\w+)\} {bar}
  • .* moo {mar}

- :

List<String> matchList = new ArrayList<String>();

Pattern regex = Pattern.compile("\\{([^}]*)\\}");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) 
{
  matchList.add(regexMatcher.group());
} 

+5

, ". *?" . (, , ) : http://javascript.about.com/library/blre09.htm

List<String> matchList = new ArrayList<String>();

Pattern regex = Pattern.compile("\\{(.*?)\\}");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group());
}

- . comportement, @madgnome. Personnaly, , ...

+2

Source: https://habr.com/ru/post/1764152/


All Articles