在猪袋字符串中获取元组的最有效方法是什么?

时间:2016-11-18 10:06:40

标签: java arrays string apache-pig

我有一个猪链袋。以下是一些可能的猪袋格式。

 {(Kumar,39)},(Raja, 30), (Mohammad, 45),{(balu,29)}
 {(Raja, 30), (Mohammad, 45),{(balu,29)}}
 {(Raja,30),(Kumar,34)}

这里的一切都被" {}"是一个猪袋。获取所有元组并将它们插入元组对象的最有效方法是什么?元组是由"()"包围的逗号分隔值。猪袋内可装有猪袋和元组。任何帮助将非常感激。以下是我尝试过的内容。虽然看起来很笨拙。

 private static void convertStringToDataBag(String dataBagString) {
    Map<Integer,Integer> openBracketsAndClosingBrackets = new HashMap<>();
    char[] charArray = dataBagString.toCharArray();
    for (int i=0; i<charArray.length;i++) {
        if(charArray[i] == '(' || charArray[i] == '{') {
            int closeIndex = findClosingParen(dataBagString,i);
            openBracketsAndClosingBrackets.put(i,closeIndex);
            String subString = dataBagString.substring(i+1,closeIndex);
            System.out.println("sub string : " +subString);
            if(!subString.contains("(") || !subString.contains(")") || !subString.contains("{") || !subString.contains("}"))) {
                //consider this as a tuple and comma split and insert.
            }
        }
    }
}

 public static int findClosingParen(String str, int openPos) {
    char[] text = str.toCharArray();
    int closePos = openPos;
    int counter = 1;
    while (counter > 0) {
        char c = text[++closePos];
        if (c == '(' || c== '{') {
            counter++;
        }
        else if (c == ')' || c== '}') {
            counter--;
        }
    }
    return closePos;
}

1 个答案:

答案 0 :(得分:1)

这应该适合你:

public static void main(String[] args) throws Exception {
    String s = "{(Kumar,39)},(Raja, 30), (Mohammad, 45),{(balu,29)}";
    // Create / compile a pattern that captures everything between each "()"
    Pattern p = Pattern.compile("\\((.*?)\\)");
   //Create a matcher using the pattern and your input string. 
    Matcher m = p.matcher(s);
   // As long as there are matches for that pattern, find them and print them.
    while(m.find()) {
        System.out.println(m.group(1)); // print data within each "()"
    }
}

O / P:

Kumar,39
Raja, 30
Mohammad, 45
balu,29