1

我有一个字符串说:

<encoded:2,Message request>

现在我想从上面的行中提取2Message request

private final String pString = "<encoded:[0-9]+,.*>";
    private final Pattern pattern = Pattern.compile(pString);

    private void parseAndDisplay(String line) {

        Matcher matcher = pattern.matcher(line);
        if (matcher.matches()) {
            while(matcher.find()) {
                String s = matcher.group();
                System.out.println("=====>"+s);

            }
        }
    }

这不会检索它。它有什么问题

4

4 回答 4

6

您必须在您的正则表达式中定义组:

"<encoded:([0-9]+),(.*?)>"

或者

"<encoded:(\\d+),([^>]*)"
于 2013-03-25T09:44:18.937 回答
4

尝试

    String s = "<encoded:2,Message request>";
    String s1 = s.replaceAll("<encoded:(\\d+?),.*", "$1");
    String s2 = s.replaceAll("<encoded:\\d+?,(.*)>", "$1");
于 2013-03-25T09:53:57.773 回答
0

尝试

"<encoded:([0-9]+),([^>]*)"

此外,正如其他评论中所建议的,使用group(1)group(2)

于 2013-03-25T09:48:38.323 回答
0

试试这个:

         Matcher matcher = Pattern.compile("<encoded:(\\d+)\\,([\\w\\s]+)",Pattern.CASE_INSENSITIVE).matcher("<encoded:2,Message request>");

    while (matcher.find()) {
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
    }
于 2013-03-25T09:58:43.927 回答