在这里,我想过滤掉这个字符串中的成功条目

时间:2015-09-24 06:10:53

标签: java pattern-matching

 String = "Success Entries and Failed Entries: {FAILED_ENTRIES=[], SUCCESS_ENTRIES=[1509230024960163905]}";

这里我只想过滤掉

  

SUCCESS_ENTRIES喜欢 1509230024960163905

String str = "Success Entries and Failed Entries: {FAILED_ENTRIES=[], SUCCESS_ENTRIES=[1509230024960163905,1509181140480153332]}";
Matcher matcher = Pattern.compile("(?<=(SUCCESS_ENTRIES=\[)).+?(?=\])").matcher(str);
matcher.find();
str = matcher.group();
System.out.println(str);
matcher = Pattern.compile("(?<=\\d{3}=)\\d+").matcher(str);
while (matcher.find()) {
    System.out.println(matcher.group());
}
}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

试试这个

String str = "Success Entries and Failed Entries: {FAILED_ENTRIES=[1509230024960163905], SUCCESS_ENTRIES=[1509230024960163905,1509181140480153332]}";
Pattern p = Pattern.compile("SUCCESS_ENTRIES=\\[(.*)\\]");
Matcher m = p.matcher(str);
for(String s: m.group(1).split("[^0-9]+"))
    if(!s.isEmpty())
        System.out.println(s);

答案 1 :(得分:0)

我的朋友很容易。您应该使用正则表达式模式。您可以创建类似这样的内容http://ideone.com/IRIeYg请查看以下示例。如果您不需要BigInteger,请轻松使用Integer.valueOf(..)

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;

class MyClass
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String str1 = "Success Entries and Failed Entries: {FAILED_ENTRIES=[], SUCCESS_ENTRIES=[1509230024960163905,1509181140480153332]}";
        String str2 = "Success Entries and Failed Entries: {FAILED_ENTRIES=[435435435], SUCCESS_ENTRIES=[1509230024960163912]}";
        String pattern = "(Success[\\s]*Entries[\\s]*and[\\s]*Failed[\\s]*Entries[\\:]+"+
                 "[\\s]*['{']+FAILED_ENTRIES['=']+[\\[0-9\\]\\,]+[\\s]*" + 
                 "SUCCESS_ENTRIES['=']+[\\[]+)?(([\\s]?[\\,]+[0-9]*)?[\\]\\}]+)?";
        BigInteger successEntires1 = new BigInteger(str1.replaceAll(pattern, ""));
        BigInteger successEntires2 = new BigInteger(str2.replaceAll(pattern, ""));
        System.out.println(successEntires1);
        System.out.println(successEntires2);
    }
}