Java:解析字符串以查找括号的内容

xde*_*ltx 1 java parsing split brackets hashtable

嗨,我是一个初学者,我一直试图让我的头围绕这一段时间,我想知道任何人都可以帮助我.

我有一个String,例如

The quick <brown> fox jumps over the <lazy> dog
Run Code Online (Sandbox Code Playgroud)

我需要做的是,在字符串中搜索每个<>括号的内容,并将它们放入哈希表中,但我无法找到一个干净的方法来获取每个单词.

任何帮助都会很棒,谢谢

kga*_*ron 6

String yourString = "The quick <brown> fox jumps over the <lazy> dog";
Pattern pattern = Pattern.compile("<(.*?)>");
Matcher matcher = pattern.matcher(yourString);
while(matcher.find()){
   String word = matcher.group(1);
   // do something with the word (like putting it in your hashtable)
}
Run Code Online (Sandbox Code Playgroud)