mayooran mayooran - 16 days ago 5
Apache Configuration Question

What is the most efficient method to get tuples within a pig bag string?

I have a string pig bag. Below are some of the possible pig bag formats.

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


Here everything surrounded by a "{}" is a pig bag. What is the most efficient way to get all the tuples and insert them into a tuple object? Tuples are the comma separated values surrounded by "()". Pig bags can contain pig bags within them along with tuples. Any help would be much appreciated. Below is what I've tried. Seems a clumsy approach though.

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;
}

Answer

This should work for you :

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
Comments