谁能帮我解决这个一个字符串变成一维字符串数组的问题

fantasy0126 2010-12-14 05:36:16
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";

s = s.substring(1, s.length() - 1);

s = s.replaceAll("\\s+", "");

String[] ss = Pattern.compile("(?<!([{][0-9,]{1,1000}[{](([{][0-9,]{1,1000}[}])|([,0-9]{1,1000}))[}])|([{][0-9,]{1,1000})),(?=([0-9]+(,[0-9]+)*([^}]|$))|(\\{([0-9]+)|(\\{[0-9]+(,[0-9]+)*\\})(,([0-9]+)|(\\{[0-9]+(,[0-9]+)*\\}))*\\}))").split(s);

for (String sa : ss) {

System.out.println(sa);

}

运行结果

1

2

{3,4,5}

{2,3}

4

{3,{4,5},5}

3

{1,2,{2,3},4,3}

6

缺点:

1)只能处理两层最多嵌套

2)该两层嵌套里面比如上面第6个分组,像{4,5}这样的只能有一组

缺点2是致命的..但向前匹配不能用+号.郁闷,也不能([0-9,]{1,1000}){1,1000}
...全文
224 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
liumoujie3 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用楼主 fantasy0126 的回复:]
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";

s = s.substring(1, s.length() - 1);

s = s.replaceAll("\\s+", "");

String[] ss = Pattern.compile("(?……
[/Quote]

说说思路吧
首先,将第一个“{”和最后一个“}”去掉
然后依次向后取数据,如果是遇到“,”并且前面没有遇到“{”,直接取出数据
如果遇到“{”则找到与此“{”对应的“}”,取出中间的内容包括“{}”
fantasy0126 2010-12-15
  • 打赏
  • 举报
回复
这个代码是根据5楼的基础上改出来的,基本实现一个字符串,转化成3级int型数组.


感谢5楼,感谢2楼..栈的思路就是强大
fantasy0126 2010-12-15
  • 打赏
  • 举报
回复
/*
*详细需求就是将一个非规范的字符串格式的3 维int 型数组,转化为真正的3维int型数组
*谁能给出一个任意维数(小于100)的实现?可能要递归?太高维数的int型数组也不是太好定义..哎
*
*/

public class Test {

public static List<String> parseStr(String s) {
List<String> list = new ArrayList<String>();

s = s.replaceAll("\\s+", "");
char[] arr = s.toCharArray();
int offset = 1;
int len = 0;
int deep = 0;
for (int i = 1; i < arr.length - 1; i++) {
if (arr[i] == '{') {
deep++;
} else if (arr[i] == '}') {
deep--;

}
len++;
if (deep == 0 && (arr[i + 1] == ',' || arr[i + 1] == '}')) {
String sn = new String(arr, offset, len);
System.out.println(sn);
list.add(sn);
offset += len + 1;
len = 0;
i++;

}


}
return list;
}
public static void printArray(int[][][] intArray){
System.out.println("********************");
for(int i=0;i<intArray.length;i++){
for(int j=0;j<intArray[i].length;j++){
for(int k=0;k<intArray[i][j].length;k++){
System.out.print(intArray[i][j][k]);
if(k<intArray[i][j].length-1)
System.out.print(",");
}
System.out.println();
}
System.out.println("----------------------");
}
}
public static void main(String[] a) throws Exception {


String s1, s2, s3;
String s = "{11,102,{3,434,{5,6,7,8,9,102,33,8}},{12,3},4,{3,{411,5},{233,4},5},3,{1,2,{2,3},4,3},6}";
List<String> c1 = parseStr(s);
int[][][] intArray = new int[c1.size()][][];
for (int i1 = 0; i1 < c1.size(); i1++) {
s1 = c1.get(i1);
if (s1.contains("{")) {
List<String> c2 = parseStr(s1);
intArray[i1] = new int[c2.size()][];
for (int i2 = 0; i2 < c2.size(); i2++) {
s2 = c2.get(i2);
if (s2.contains("{")) {
List<String> c3 = parseStr(s2);
intArray[i1][i2] = new int[c3.size()];
for (int i3 = 0; i3 < c3.size(); i3++) {
s3 = c3.get(i3);
intArray[i1][i2][i3] = Integer.parseInt(s3);

}

} else {
intArray[i1][i2] = new int[1];
intArray[i1][i2][0] = Integer.parseInt(s2);
}


}


} else {
intArray[i1] = new int[1][1];
intArray[i1][0][0] = Integer.parseInt(s1);
}

}

printArray(intArray);

}
}
fantasy0126 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 shaosijun2004 的回复:]

public class Test {
static int deep = 0;
public static void main(String[] a) throws Exception{
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
s =s.replace(",", "");
char[] a……
[/Quote]

5楼思路不错,可是11就变成1,1了
yaoweijq 2010-12-15
  • 打赏
  • 举报
回复
没看懂,求详细需求。。。
[Quote=引用 7 楼 fantasy0126 的回复:]
4,6楼要仔细看题目~
俺 不是那个意思
[/Quote]
fantasy0126 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 caofaping 的回复:]

Java code

public static void main(String[] args) {
//数据一
// String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
//数据二
String s = "{1,2,{{2,3},3,4,5,{……
[/Quote]

3楼很强大的代码...我这里测完美实现两层或3层,..就是看不太懂逻辑....我很菜...佩服啊
fantasy0126 2010-12-15
  • 打赏
  • 举报
回复
4,6楼要仔细看题目~
俺 不是那个意思
random0406 2010-12-14
  • 打赏
  • 举报
回复
public static void main(String[] args) {
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
s = s.replaceAll("\\{|\\}+", "");
System.out.println(s);
String ss[]=s.split(",");
for (String sa : ss) {
System.out.println(sa);
}
}

这样做符合要求不?
shaosijun2004 2010-12-14
  • 打赏
  • 举报
回复
public class Test {
static int deep = 0;
public static void main(String[] a) throws Exception{
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
s =s.replace(",", "");
char[] arr = s.toCharArray();
int offset = 1;
int len = 0;
for(int i=1;i<arr.length-1;i++){
if(arr[i]=='{')
{
deep++;
}
else
if(arr[i]=='}')
{
deep--;

}
len++;
if(deep==0){
System.out.println(new String(arr,offset,len));
offset += len;
len=0;
}


}
}

}
像楼主那样输出的……这方法比较2
楼主要输出所有的集合么?
eXeSP 2010-12-14
  • 打赏
  • 举报
回复
如果只包含 "{ } ,",或许可以用点暴力的办法

public static void main(String[] args) {
String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
String ss = s.replaceAll("\\{|\\}|\\,", "");
int[] n = new int[ss.length()];
for(int i = 0;i<ss.length();i++){
n[i] = Integer.parseInt(ss.substring(i,i+1));
System.out.println(n[i]);
}
}
}

caofaping 2010-12-14
  • 打赏
  • 举报
回复

public static void main(String[] args) {
//数据一
// String s = "{1,2,{3,4,5},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
//数据二
String s = "{1,2,{{2,3},3,4,5,{2,3}},{2,3},4,{3,{4,5},5},3,{1,2,{2,3},4,3},6}";
s = s.substring(1, s.length() - 1);
System.out.println(Arrays.asList(s.split(",")));
parseArr(check(s.split(",")));
}

private static void parseArr(List<String> sou) {
List<String> list = new ArrayList<String>();
StringBuffer sb = new StringBuffer();
int len = 0;// {}匹配
boolean b = false;// 用来判断前面是否出线{},
boolean lenBoo = false;// 用来判断是否出现{}
boolean isLast = false;// 用来判断添加,
for (int i = 0; i < sou.size(); i++) {
String s = sou.get(i);
if (s.indexOf("{") > -1) {
b = true;
lenBoo = true;
len++;
}
if (s.indexOf("}") > -1) {
lenBoo = true;
len--;
}

if (len > 0) {
lenBoo = true;
}

if (len == 0 && lenBoo) {
isLast = true;
}

if (lenBoo) {
sb.append(s);
if (!isLast) {
sb.append(",");
isLast = false;
}
lenBoo = false;
}
// 不在括号里面或括号已经匹配成功
if (len == 0) {
// 如果前面是否出现{}
if (!b) {
list.add(s);
} else {
list.add(sb.toString().replaceAll("\\{,\\{","{{").replaceAll("\\},\\}", "}}"));
}
sb.delete(0, sb.length());
b = false;
isLast = false;
}
// System.out.print(list);
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
}

private static List<String> check(String[] arr) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < arr.length; i++) {
String s = arr[i];
if (s.startsWith("{{")) {
list.add("{");
list.add(s.substring(1, s.length()));
continue;
}
if (s.endsWith("}}")) {
list.add(s.substring(0, s.length()-1));
list.add("}");
continue;
}
list.add(s);
}
return list;
}


没用正则给你个参考。可以自己改进下。
Jlins 2010-12-14
  • 打赏
  • 举报
回复
使用栈 使用栈 使用栈

这种问题使用栈 最简单

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧