回答編集履歴
3
修正
test
CHANGED
@@ -92,9 +92,9 @@
|
|
92
92
|
|
93
93
|
split() を使わずに正規表現で数字部分を取り出す方法で書いてみました。
|
94
94
|
|
95
|
-
|
95
|
+
0 - 1999 の値の範囲の数値であることも正規表現でチェックすることは可能ですが、
|
96
96
|
|
97
|
-
範囲が
|
97
|
+
範囲が 3 - 1998 なんてふうになったときに対応が困難です。
|
98
98
|
|
99
99
|
数値範囲については、parseInt() してチェックするようにしています。
|
100
100
|
|
2
追記
test
CHANGED
@@ -85,3 +85,113 @@
|
|
85
85
|
実行例:
|
86
86
|
|
87
87
|

|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
追記:
|
92
|
+
|
93
|
+
split() を使わずに正規表現で数字部分を取り出す方法で書いてみました。
|
94
|
+
|
95
|
+
1 - 1999 の値の範囲の数値であることも正規表現でチェックすることは可能ですが、
|
96
|
+
|
97
|
+
範囲が 99 - 1998 なんてふうになったときに対応が困難です。
|
98
|
+
|
99
|
+
数値範囲については、parseInt() してチェックするようにしています。
|
100
|
+
|
101
|
+
(正規表現で 4 桁以内の数字列しか渡らないはずなので parse 時の Format 不正例外は発生しないはず)
|
102
|
+
|
103
|
+
```java
|
104
|
+
|
105
|
+
package teratail;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
import java.util.regex.Matcher;
|
110
|
+
|
111
|
+
import java.util.regex.Pattern;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
public class Parse {
|
116
|
+
|
117
|
+
// 数字を含んでいるか?
|
118
|
+
|
119
|
+
private final static Pattern pattern_0 = Pattern.compile("\d");
|
120
|
+
|
121
|
+
// "数値s数値s ... 数値" のパターンか?
|
122
|
+
|
123
|
+
private final static Pattern pattern_1 = Pattern.compile("^((\d{1,4})s)+(\d{1,4})$");
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
public static void main(String[] args) {
|
128
|
+
|
129
|
+
String STRS[] = {
|
130
|
+
|
131
|
+
"abcsABC",
|
132
|
+
|
133
|
+
"123",
|
134
|
+
|
135
|
+
"1111s334",
|
136
|
+
|
137
|
+
"1s2s3s4s5s6s7s8s9",
|
138
|
+
|
139
|
+
"1111s334s124s215s126",
|
140
|
+
|
141
|
+
"1111s334s124s215s1999",
|
142
|
+
|
143
|
+
"1111s334s124s215s2000",
|
144
|
+
|
145
|
+
"12ss3",
|
146
|
+
|
147
|
+
"1.2sabc",
|
148
|
+
|
149
|
+
"99999999999999999999999999999999999999999999999999s1"
|
150
|
+
|
151
|
+
};
|
152
|
+
|
153
|
+
for (String str : STRS) {
|
154
|
+
|
155
|
+
if (pattern_0.matcher(str).find()) {
|
156
|
+
|
157
|
+
System.out.println(check(str, pattern_1) + ":\t'" + str + "'");
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
static boolean check(String str, Pattern pattern) {
|
168
|
+
|
169
|
+
Matcher m = pattern.matcher(str);
|
170
|
+
|
171
|
+
if (!m.find()) {
|
172
|
+
|
173
|
+
return false;
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
int count = m.groupCount();
|
178
|
+
|
179
|
+
for (int i = 1; i <= count; i++) {
|
180
|
+
|
181
|
+
int x = Integer.parseInt(m.group(i).replace("s", ""));
|
182
|
+
|
183
|
+
if ((x < 0) || (1999 < x)) {
|
184
|
+
|
185
|
+
return false;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
return true;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
```
|
1
code のペースト
test
CHANGED
@@ -3,6 +3,76 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
```java
|
6
|
+
|
7
|
+
import java.util.regex.Pattern;
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
public class Parse {
|
12
|
+
|
13
|
+
public static void main(String[] args) {
|
14
|
+
|
15
|
+
String STRS[] = {
|
16
|
+
|
17
|
+
"abcsABC",
|
18
|
+
|
19
|
+
"123",
|
20
|
+
|
21
|
+
"1111s334s124s214s124",
|
22
|
+
|
23
|
+
"12ss3",
|
24
|
+
|
25
|
+
"1.2sabc",
|
26
|
+
|
27
|
+
"99999999999999999999999999999999999999999999999999s1"
|
28
|
+
|
29
|
+
};
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
Pattern pattern = Pattern.compile("\d");
|
34
|
+
|
35
|
+
for (String str : STRS) {
|
36
|
+
|
37
|
+
if (pattern.matcher(str).find()) {
|
38
|
+
|
39
|
+
System.out.println(check(str) + ":\t'" + str + "'");
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
static boolean check(String str) {
|
50
|
+
|
51
|
+
for (String num_str : str.split("s")) {
|
52
|
+
|
53
|
+
try {
|
54
|
+
|
55
|
+
int x = Integer.parseInt(num_str);
|
56
|
+
|
57
|
+
if ((x < 0) || (1999 < x)) {
|
58
|
+
|
59
|
+
return false;
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
} catch (NumberFormatException e) {
|
64
|
+
|
65
|
+
return false;
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
return true;
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
}
|
6
76
|
|
7
77
|
|
8
78
|
|