本帖最后由 老衲玩IT 于 2013-8-28 16:28 编辑
楼主看好啦,首先是按正则式用x,y隔离出符合规则的字符串,再把被x或y包围的字符串替换成指定的字符串。- String str="11000 1100000111 1100000111 00011111000100011";
- str=str.replaceAll("(1{2}0{3})", "x$1x");
- System.out.println(str);
- str=str.replaceAll("(0{3}1{2})", "y$1y");
- System.out.println(str);
- str=str.replaceAll("x1{2}0{3}x", " 10 ");
- System.out.println(str);
- str=str.replaceAll("y0{3}1{2}y", " 01 ");
- System.out.println(str);
复制代码 输出结果是这样的:
x11000x x11000x00111 x11000x00111 000111x11000x100011
x11000x x11000x00111 x11000x00111 y00011y1x11000x1y00011y
10 10 00111 10 00111 y00011y1 10 1y00011y
10 10 00111 10 00111 01 1 10 1 01 |