在猪包字符串中获取元组的最有效方法是什么?
What is the most efficient method to get tuples within a pig bag string?
我有一个串猪包。以下是一些可能的猪包格式。
{(Kumar,39)},(Raja, 30), (Mohammad, 45),{(balu,29)}
{(Raja, 30), (Mohammad, 45),{(balu,29)}}
{(Raja,30),(Kumar,34)}
这里所有被“{}”包围的都是猪包。获取所有元组并将它们插入元组对象的最有效方法是什么?元组是由“()”包围的逗号分隔值。 Pig bags 可以在其中包含 pig bags 以及元组。任何帮助将非常感激。以下是我尝试过的。不过,这似乎是一种笨拙的方法。
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;
}
这应该适合你:
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
我有一个串猪包。以下是一些可能的猪包格式。
{(Kumar,39)},(Raja, 30), (Mohammad, 45),{(balu,29)}
{(Raja, 30), (Mohammad, 45),{(balu,29)}}
{(Raja,30),(Kumar,34)}
这里所有被“{}”包围的都是猪包。获取所有元组并将它们插入元组对象的最有效方法是什么?元组是由“()”包围的逗号分隔值。 Pig bags 可以在其中包含 pig bags 以及元组。任何帮助将非常感激。以下是我尝试过的。不过,这似乎是一种笨拙的方法。
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;
}
这应该适合你:
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