回答編集履歴

2

追記

2020/08/20 10:19

投稿

yureighost
yureighost

スコア2183

test CHANGED
@@ -57,3 +57,77 @@
57
57
  */
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ **追記**
64
+
65
+ すみません。
66
+
67
+ 確かにこれだと整数4、5桁でも通ってしまいますね。
68
+
69
+ こちらで
70
+
71
+ ```java
72
+
73
+ Pattern pt = Pattern.compile("^([1-9][0-9]{0,2}|0)(\.[0-9]{1,2})?$");
74
+
75
+
76
+
77
+ String num1 = "1";
78
+
79
+ String num2 = "1.1";
80
+
81
+ String num3 = "123";
82
+
83
+ String num4 = "1.12";
84
+
85
+ String num5 = "1234";
86
+
87
+ String num6 = "123.12";
88
+
89
+ String num7 = "1234.12";
90
+
91
+ String num8 = "123.123";
92
+
93
+
94
+
95
+ System.out.println(pt.matcher(num1).find());
96
+
97
+ System.out.println(pt.matcher(num2).find());
98
+
99
+ System.out.println(pt.matcher(num3).find());
100
+
101
+ System.out.println(pt.matcher(num4).find());
102
+
103
+ System.out.println(pt.matcher(num5).find());
104
+
105
+ System.out.println(pt.matcher(num6).find());
106
+
107
+ System.out.println(pt.matcher(num7).find());
108
+
109
+ System.out.println(pt.matcher(num8).find());
110
+
111
+
112
+
113
+ /*
114
+
115
+ true
116
+
117
+ true
118
+
119
+ true
120
+
121
+ true
122
+
123
+ false
124
+
125
+ true
126
+
127
+ false
128
+
129
+ false
130
+
131
+ */
132
+
133
+ ```

1

ソース修正

2020/08/20 10:19

投稿

yureighost
yureighost

スコア2183

test CHANGED
@@ -2,15 +2,23 @@
2
2
 
3
3
  ```java
4
4
 
5
- Pattern pt = Pattern.compile("^\d{1,3}\.\d{1,2}$");
5
+ Pattern pt = Pattern.compile("^\d{1,3}\.?\d{0,2}$");
6
6
 
7
7
 
8
8
 
9
- String num1 = "123.12";
9
+ String num1 = "1";
10
10
 
11
- String num2 = "1234.12";
11
+ String num2 = "1.1";
12
12
 
13
+ String num3 = "123";
14
+
15
+ String num4 = "1.12";
16
+
17
+ String num5 = "123.12";
18
+
19
+ String num6 = "1234.12";
20
+
13
- String num3 = "123.123";
21
+ String num7 = "123.123";
14
22
 
15
23
 
16
24
 
@@ -20,9 +28,25 @@
20
28
 
21
29
  System.out.println(pt.matcher(num3).find());
22
30
 
31
+ System.out.println(pt.matcher(num4).find());
32
+
33
+ System.out.println(pt.matcher(num5).find());
34
+
35
+ System.out.println(pt.matcher(num6).find());
36
+
37
+ System.out.println(pt.matcher(num7).find());
38
+
23
39
 
24
40
 
25
41
  /*
42
+
43
+ true
44
+
45
+ true
46
+
47
+ true
48
+
49
+ true
26
50
 
27
51
  true
28
52