【发布时间】:2014-04-10 17:21:32
【问题描述】:
我需要根据分隔符拆分字符串并将其分配给对象。我知道拆分功能,但我无法弄清楚如何为我的特定字符串执行此操作。
对象的格式为:
class Selections{
int n;
ArrayList<Integer> choices;
}
字符串的格式为:
1:[1,3,2],2:[1],3:[4,3],4:[4,3]
在哪里:
1:[1,3,2] is an object with n=1 and Arraylist should have numbers 1,2,3.
2:[1] is an object with n=2 and Arraylist should have number 1
等等。
我不能使用“,”作为分隔符的拆分,因为单个对象和 [] 中的元素都用“,”分隔。
任何想法都将不胜感激。
【问题讨论】:
-
为什么不直接使用
],作为分隔符?为了获得额外奖励,请使用正则表达式,该正则表达式仅在]前面时才在,上拆分。 -
要实现@Gabe 的建议,请查看Stack Overflow Regular Expressions FAQ 中的“Lookarounds”部分,尤其是
(?<=...):positive lookbehinds。