【问题标题】:Java: Parse string to find the content of bracketsJava:解析字符串以查找括号的内容
【发布时间】:2012-08-10 09:19:27
【问题描述】:

您好,我是一个初学者,我一直在努力解决这个问题,我想知道谁能帮我一把。

我有一个字符串,例如

The quick <brown> fox jumps over the <lazy> dog

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

任何帮助都会很棒,谢谢

【问题讨论】:

  • 你试过正则表达式吗?你尝试了哪些不干净的方法?
  • 我尝试反复获取 的索引,但很快就变得一团糟

标签: java parsing split hashtable brackets


【解决方案1】:
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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 2021-11-18
    • 2020-05-14
    • 1970-01-01
    • 2014-09-09
    • 2015-05-05
    • 1970-01-01
    • 2014-05-20
    相关资源
    最近更新 更多