Posted on 2009-07-20 08:50
java人生 阅读(1474)
评论(0) 编辑 收藏 所属分类:
javaSE
自己用java做了一个类似红蜻蜓截图的软件,里面要制作文件命名的模板,
如:
#aa##bb###
#为需要用指定序号替换的字符,10起始值
连续#个数小于等于起始值位数,#被起始值直接替换
连续#个数大于起始值位数时,#被起始值替换并且多出的#用0替换
1import java.util.regex.Matcher;
2import java.util.regex.Pattern;
3
4
5public class Test {
6
7 public static void main(String[] args) {
8 String template = "#a##b###c";
9 String show1 = null, show2 = null, show3 = null;
10 int start = 10;
11 String startStr = String.valueOf(start);
12
13 String regex = "#{1,}";
14 String[] splitStr = template.split(regex);
15 if (template.indexOf("#") == -1) {
16 show1 = template + start;
17 show2 = template + (start + 1);
18 show3 = template + (start + 2);
19 } else {
20 if (splitStr.length == 0) {
21 if (String.valueOf(start).length() >= template.length())
22 show1 = String.valueOf(start);
23 else {
24 String s = template.substring(0, template.length()
25 - String.valueOf(start).length());
26 show1 = (s + start).replace("#", "0");
27 }
28 if (String.valueOf(start + 1).length() >= template.length()) {
29 show2 = String.valueOf(start + 1);
30 } else {
31 String s = template.substring(0, template.length()
32 - String.valueOf(start + 1).length());
33 show2 = (s + (start + 1)).replace("#", "0");
34 }
35 if (String.valueOf(start + 2).length() >= template.length()) {
36 show3 = String.valueOf(start + 2);
37 } else {
38 String s = template.substring(0, template.length()
39 - String.valueOf(start + 2).length());
40 show3 = (s + (start + 2)).replace("#", "0");
41 }
42 } else {
43 Pattern pattern = Pattern.compile(regex);
44 Matcher match = pattern.matcher(template);
45
46 StringBuffer buffer1 = new StringBuffer();
47 StringBuffer buffer2 = new StringBuffer();
48 StringBuffer buffer3 = new StringBuffer();
49
50 int index = 0;
51 while (match.find()) {
52 String rules = match.group();
53 buffer1.append(splitStr[index]);
54 buffer2.append(splitStr[index]);
55 buffer3.append(splitStr[index]);
56
57 if (rules.length() <= String.valueOf(start).length()) {
58 buffer1.append(start);
59 } else {
60 String s = rules.substring(0, rules.length()
61 - startStr.length());
62
63 buffer1.append((s + start).replace("#", "0"));
64 }
65 if (rules.length() <= String.valueOf(start + 1).length()) {
66 buffer2.append(start + 1);
67 } else {
68 String s = rules.substring(0, rules.length()
69 - String.valueOf(start + 1).length());
70 String tmp2 = (s + (start + 1)).replace("#", "0");
71
72 buffer2.append(tmp2);
73 }
74 if (rules.length() <= String.valueOf(start + 2).length()) {
75 buffer3.append(start + 2);
76 } else {
77 String s = rules.substring(0, rules.length()
78 - String.valueOf(start + 1).length());
79 String tmp3 = (s + (start + 2)).replace("#", "0");
80
81 buffer3.append(tmp3);
82 }
83 index++;
84 }
85 if (index < splitStr.length) {
86 buffer1.append(splitStr[index]);
87 buffer2.append(splitStr[index]);
88 buffer3.append(splitStr[index]);
89 }
90 show1 = buffer1.toString();
91 show2 = buffer2.toString();
92 show3 = buffer3.toString();
93 }
94 }
95 System.out.println("show1: "+show1);
96 System.out.println("show2: "+show2);
97 System.out.println("show3: "+show3);
98 }
99}
100