fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.regex.Pattern;
  4.  
  5. class TesteRegex {
  6.  
  7. private static final Pattern PERC_PAT =
  8. Pattern.compile("(?:[1-9]|[1-9][0-9]|100)%?");
  9.  
  10. private static String format(String in) {
  11. if (in == null) return "";
  12. for (String s = in; !s.isEmpty(); s = s.substring(0, s.length() - 1)) {
  13. if (PERC_PAT.matcher(s).matches()) return s;
  14. }
  15. return "";
  16. }
  17.  
  18. public static void main(String[] args) {
  19. List<String> s = new ArrayList<>();
  20. s.add("");
  21. s.add("%");
  22. s.add("x");
  23. for (int i = 0; i < 100; i++) {
  24. s.add(i + "");
  25. s.add(i + "%");
  26. s.add(i + "x");
  27. }
  28. for (int i = 0; i < 11; i++) {
  29. s.add(i + "00");
  30. s.add(i + "00%");
  31. s.add(i + "00x");
  32. s.add(i + "01");
  33. s.add(i + "01%");
  34. s.add(i + "01x");
  35. s.add("0" + i);
  36. }
  37. for (String ss : s) {
  38. System.out.println(ss + ": " + format(ss));
  39. }
  40. }
  41. }
Success #stdin #stdout 0.13s 29496KB
stdin
Standard input is empty
stdout
: 
%: 
x: 
0: 
0%: 
0x: 
1: 1
1%: 1%
1x: 1
2: 2
2%: 2%
2x: 2
3: 3
3%: 3%
3x: 3
4: 4
4%: 4%
4x: 4
5: 5
5%: 5%
5x: 5
6: 6
6%: 6%
6x: 6
7: 7
7%: 7%
7x: 7
8: 8
8%: 8%
8x: 8
9: 9
9%: 9%
9x: 9
10: 10
10%: 10%
10x: 10
11: 11
11%: 11%
11x: 11
12: 12
12%: 12%
12x: 12
13: 13
13%: 13%
13x: 13
14: 14
14%: 14%
14x: 14
15: 15
15%: 15%
15x: 15
16: 16
16%: 16%
16x: 16
17: 17
17%: 17%
17x: 17
18: 18
18%: 18%
18x: 18
19: 19
19%: 19%
19x: 19
20: 20
20%: 20%
20x: 20
21: 21
21%: 21%
21x: 21
22: 22
22%: 22%
22x: 22
23: 23
23%: 23%
23x: 23
24: 24
24%: 24%
24x: 24
25: 25
25%: 25%
25x: 25
26: 26
26%: 26%
26x: 26
27: 27
27%: 27%
27x: 27
28: 28
28%: 28%
28x: 28
29: 29
29%: 29%
29x: 29
30: 30
30%: 30%
30x: 30
31: 31
31%: 31%
31x: 31
32: 32
32%: 32%
32x: 32
33: 33
33%: 33%
33x: 33
34: 34
34%: 34%
34x: 34
35: 35
35%: 35%
35x: 35
36: 36
36%: 36%
36x: 36
37: 37
37%: 37%
37x: 37
38: 38
38%: 38%
38x: 38
39: 39
39%: 39%
39x: 39
40: 40
40%: 40%
40x: 40
41: 41
41%: 41%
41x: 41
42: 42
42%: 42%
42x: 42
43: 43
43%: 43%
43x: 43
44: 44
44%: 44%
44x: 44
45: 45
45%: 45%
45x: 45
46: 46
46%: 46%
46x: 46
47: 47
47%: 47%
47x: 47
48: 48
48%: 48%
48x: 48
49: 49
49%: 49%
49x: 49
50: 50
50%: 50%
50x: 50
51: 51
51%: 51%
51x: 51
52: 52
52%: 52%
52x: 52
53: 53
53%: 53%
53x: 53
54: 54
54%: 54%
54x: 54
55: 55
55%: 55%
55x: 55
56: 56
56%: 56%
56x: 56
57: 57
57%: 57%
57x: 57
58: 58
58%: 58%
58x: 58
59: 59
59%: 59%
59x: 59
60: 60
60%: 60%
60x: 60
61: 61
61%: 61%
61x: 61
62: 62
62%: 62%
62x: 62
63: 63
63%: 63%
63x: 63
64: 64
64%: 64%
64x: 64
65: 65
65%: 65%
65x: 65
66: 66
66%: 66%
66x: 66
67: 67
67%: 67%
67x: 67
68: 68
68%: 68%
68x: 68
69: 69
69%: 69%
69x: 69
70: 70
70%: 70%
70x: 70
71: 71
71%: 71%
71x: 71
72: 72
72%: 72%
72x: 72
73: 73
73%: 73%
73x: 73
74: 74
74%: 74%
74x: 74
75: 75
75%: 75%
75x: 75
76: 76
76%: 76%
76x: 76
77: 77
77%: 77%
77x: 77
78: 78
78%: 78%
78x: 78
79: 79
79%: 79%
79x: 79
80: 80
80%: 80%
80x: 80
81: 81
81%: 81%
81x: 81
82: 82
82%: 82%
82x: 82
83: 83
83%: 83%
83x: 83
84: 84
84%: 84%
84x: 84
85: 85
85%: 85%
85x: 85
86: 86
86%: 86%
86x: 86
87: 87
87%: 87%
87x: 87
88: 88
88%: 88%
88x: 88
89: 89
89%: 89%
89x: 89
90: 90
90%: 90%
90x: 90
91: 91
91%: 91%
91x: 91
92: 92
92%: 92%
92x: 92
93: 93
93%: 93%
93x: 93
94: 94
94%: 94%
94x: 94
95: 95
95%: 95%
95x: 95
96: 96
96%: 96%
96x: 96
97: 97
97%: 97%
97x: 97
98: 98
98%: 98%
98x: 98
99: 99
99%: 99%
99x: 99
000: 
000%: 
000x: 
001: 
001%: 
001x: 
00: 
100: 100
100%: 100%
100x: 100
101: 10
101%: 10
101x: 10
01: 
200: 20
200%: 20
200x: 20
201: 20
201%: 20
201x: 20
02: 
300: 30
300%: 30
300x: 30
301: 30
301%: 30
301x: 30
03: 
400: 40
400%: 40
400x: 40
401: 40
401%: 40
401x: 40
04: 
500: 50
500%: 50
500x: 50
501: 50
501%: 50
501x: 50
05: 
600: 60
600%: 60
600x: 60
601: 60
601%: 60
601x: 60
06: 
700: 70
700%: 70
700x: 70
701: 70
701%: 70
701x: 70
07: 
800: 80
800%: 80
800x: 80
801: 80
801%: 80
801x: 80
08: 
900: 90
900%: 90
900x: 90
901: 90
901%: 90
901x: 90
09: 
1000: 100
1000%: 100
1000x: 100
1001: 100
1001%: 100
1001x: 100
010: