봉봉의 개인 블로그

2017-12-01(정규 표현식) 본문

입사후 공부한내용

2017-12-01(정규 표현식)

봉봉이네 2017. 12. 1. 15:32

정규표현식은 특정한 규칙을 가지는 문자를 표현하는데 사용하는 형식언어이다.
정규표현식을 잘 모를 때에는 validation체크가 상당히 고단하고, 지루했지만 정규 표현식을 자유자재로 다룰 줄 알게 되면 상당한 시간을 단축할 수 있고, 유용하며 코드가 깔끔해 진다.

 표현식

설명 

^

  문자열의 시작 

$

  문자열의 종료 

  임의의 한 문자(문자의 종류 가리지 않음) 단 \는 넣을수 없음 

  앞 문자가 없을 수도 무한정 많을 수도 있음 

  앞 문자가 하나 이상 

  앞 문자가 없거나 하나 있음 

[] 

  문자의 집합이나 범위를 나타내며 두 문자 사이는 - 기호로 범위를 나타냄 []내에서 ^가 선행
  하여 존재하면 not을 나타낸다. 

{} 

  횟수 또는 범위를 나타낸다. 

() 

  소괄호 안의 문자를 하나의 문자로 인식 

  패턴 안에서 or 연산을 수행할때 사용 

\s 

  공백문자 

\S 

  공백문자가 아닌 나머지 문자 

\w 

  알바벳이나 숫자 

\W 

  알파벳이나 숫자를 제외한 문자

\d 

  숫자 [0-9]와 동일 

\D 

  숫자를 제외한 모든 문자 

  정규표현식 역슬래시(\)는 확장문자
  역슬래시 다음에 일반 문자가 오면 특수 문자로 취급하고 역슬래시 다음에 특수문자가 오면
  그 문자 자체를 의미 

(?i) 

  앞 부분에 (?i) 라는 옵션을 넣어주면 대소문자를 구분하지 않음 

자바스크립트 또한 자바의 정규표현식과 대동소이하다.
아래 사용법만 익히면 쉽게 사용할 수 있다.

var filter = /[a-z]+/;
if(filter.test("a")== ture){
    alert("success")
}else{
    alert("fail")
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
package regular;
 
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
 
import java.util.regex.Pattern;
 
import org.junit.Test;
 
public class RegularTest {
 
    /**
     * <pre>
     * 문자열시작테스트 ^
     *
     * <pre>
     */
    @Test
    public void 문자열시작테스트() {
        String regex = "^[a-zA-Z]{3}[0-9]+";
        assertThat(true, is(matched(regex, "aAb09")));
        assertThat(true, is(matched(regex, "aaa09")));
        assertThat(true, is(matched(regex, "AAA09")));
        assertThat(true, is(matched(regex, "zzZ09")));
        assertThat(true, is(matched(regex, "zzZ00")));
 
        assertThat(false, is(matched(regex, "zzZ")));
        assertThat(false, is(matched(regex, "12a")));
        assertThat(false, is(matched(regex, "aaaa09")));
    }
 
    /**
     * <pre>
     * 문자열의종료 $
     *
     * <pre>
     */
    @Test
    public void 문자열의종료() {
        String regex = "a{2}$";
        assertThat(true, is(matched(regex, "aa")));
 
        assertThat(false, is(matched(regex, "aa11")));
    }
 
    /**
     * <pre>
     * 임의의한문자를나타냄 .
     *
     * <pre>
     */
    @Test
    public void 임의의한문자를나타냄() {
        String regex = "aaa.bb";
        assertThat(true, is(matched(regex, "aaa1bb")));
        assertThat(true, is(matched(regex, "aaaCbb")));
        assertThat(true, is(matched(regex, "aaaZbb")));
        assertThat(true, is(matched(regex, "aaa!bb")));
        assertThat(true, is(matched(regex, "aaa@bb")));
        assertThat(true, is(matched(regex, "aaa#bb")));
        assertThat(true, is(matched(regex, "aaa$bb")));
        assertThat(true, is(matched(regex, "aaa%bb")));
        assertThat(true, is(matched(regex, "aaa^bb")));
        assertThat(true, is(matched(regex, "aaa&bb")));
        assertThat(true, is(matched(regex, "aaa*bb")));
        assertThat(true, is(matched(regex, "aaa(bb")));
        assertThat(true, is(matched(regex, "aaa)bb")));
        assertThat(true, is(matched(regex, "aaa-bb")));
        assertThat(true, is(matched(regex, "aaa+bb")));
        assertThat(true, is(matched(regex, "aaa=bb")));
 
        assertThat(false, is(matched(regex, "aaabb")));
        assertThat(false, is(matched(regex, "aaa\bb")));
        assertThat(false, is(matched(regex, "aaa12bb")));
        assertThat(false, is(matched(regex, "aaa$$bb")));
    }
 
    /**
     * <pre>
     * 앞문자가없을수도무한정있을수도있음 *
     *
     * <pre>
     */
    @Test
    public void 앞문자가없을수도무한정있을수도있음() {
        String regex = "a*b";
 
        assertThat(true, is(matched(regex, "ab")));
        assertThat(true, is(matched(regex, "aaab")));
        assertThat(true, is(matched(regex, "aab")));
        assertThat(true, is(matched(regex, "b")));
 
        assertThat(false, is(matched(regex, "aabb")));
        assertThat(false, is(matched(regex, "aaccdd")));
        assertThat(false, is(matched(regex, "a_ab")));
        assertThat(false, is(matched(regex, "abcd")));
    }
 
    /**
     * <pre>
     * 앞문자가하나이상 +
     *
     * <pre>
     */
    @Test
    public void 앞문자가하나이상() {
        String regex = "abc_A+";
 
        assertThat(true, is(matched(regex, "abc_A")));
        assertThat(true, is(matched(regex, "abc_AA")));
        assertThat(true, is(matched(regex, "abc_AAA")));
 
        assertThat(false, is(matched(regex, "abc")));
        assertThat(false, is(matched(regex, "abc_")));
        assertThat(false, is(matched(regex, "abc_AAb")));
        assertThat(false, is(matched(regex, "abc_AA1")));
    }
 
    /**
     * <pre>
     * 앞문자가없거나하나있음 ?
     *
     * <pre>
     */
    @Test
    public void 앞문자가없거나하나있음() {
        String regex = "abc_B?";
 
        assertThat(true, is(matched(regex, "abc_")));
        assertThat(true, is(matched(regex, "abc_B")));
 
        assertThat(false, is(matched(regex, "abc_C")));
        assertThat(false, is(matched(regex, "abc_DD")));
        assertThat(false, is(matched(regex, "abc_11")));
    }
 
    /**
     * <pre>
     * 문자클래스지정1 []
     *
     * <pre>
     */
    @Test
    public void 문자클래스지정1() {
        String regex = "[a-z0-9]";
 
        assertThat(true, is(matched(regex, "a")));
        assertThat(true, is(matched(regex, "b")));
        assertThat(true, is(matched(regex, "z")));
        assertThat(true, is(matched(regex, "0")));
        assertThat(true, is(matched(regex, "8")));
 
        assertThat(false, is(matched(regex, "A")));
        assertThat(false, is(matched(regex, "aa")));
        assertThat(false, is(matched(regex, "a0")));
        assertThat(false, is(matched(regex, "00a")));
    }
 
    /**
     * <pre>
     * 문자클래스지정2 [^]
     *
     * <pre>
     */
    @Test
    public void 문자클래스지정2() {
        String regex = "[^a-f1-3]";
 
        assertThat(true, is(matched(regex, "g")));
        assertThat(true, is(matched(regex, "h")));
        assertThat(true, is(matched(regex, "4")));
        assertThat(true, is(matched(regex, "0")));
        assertThat(true, is(matched(regex, "9")));
 
        assertThat(false, is(matched(regex, "a")));
        assertThat(false, is(matched(regex, "2")));
    }
 
    /**
     * <pre>
     * 횟수또는범위1 {3}
     * a 3개만 올 수 있음
     *
     * <pre>
     */
    @Test
    public void 횟수또는범위1() {
        String regex = "a{3}b";
 
        assertThat(true, is(matched(regex, "aaab")));
 
        assertThat(false, is(matched(regex, "aab")));
        assertThat(false, is(matched(regex, "aaaab")));
        assertThat(false, is(matched(regex, "aaaaaaab")));
        assertThat(false, is(matched(regex, "aaaaaaabb")));
    }
 
    /**
     * <pre>
     * 횟수또는범위2 {3,}
     * a 3개 이상 올 수 있음
     * <pre>
     */
    @Test
    public void 횟수또는범위2() {
        String regex = "a{3,}b";
 
        assertThat(true, is(matched(regex, "aaab")));
        assertThat(true, is(matched(regex, "aaaab")));
        assertThat(true, is(matched(regex, "aaaaab")));
        assertThat(true, is(matched(regex, "aaaaaaaaaaaaaaaaaaaaaaaaab")));
 
        assertThat(false, is(matched(regex, "aab")));
        assertThat(false, is(matched(regex, "ab")));
        assertThat(false, is(matched(regex, "b")));
    }
 
    /**
     * <pre>
     * 횟수또는범위3 {3,5}
     *    a 3개 이상 5개 이하 올 수 있음
     * <pre>
     */
    @Test
    public void 횟수또는범위3() {
        String regex = "a{3,5}b";
 
        assertThat(true, is(matched(regex, "aaab")));
        assertThat(true, is(matched(regex, "aaaab")));
        assertThat(true, is(matched(regex, "aaaaab")));
 
        assertThat(false, is(matched(regex, "aaaaaab")));
        assertThat(false, is(matched(regex, "aaaaaaaaaaab")));
 
        String regex1 = "a{3,5}.";
        assertThat(true, is(matched(regex1, "aaab")));
        assertThat(false, is(matched(regex1, "aaabb")));
 
        String regex2 = "a{3,5}.{4}";
        assertThat(true, is(matched(regex2, "aaabbbb")));
        assertThat(false, is(matched(regex2, "aaabbbbb")));
        assertThat(false, is(matched(regex2, "aaab")));
        assertThat(false, is(matched(regex2, "aaabb")));
        assertThat(false, is(matched(regex2, "aaabbb")));
    }
 
    /**
     * <pre>
     * 소괄호문자를하나의문자로인식 ()
     *
     * <pre>
     */
    @Test
    public void 소괄호문자를하나의문자로인식() {
        String regex = "abc(11){2}";
 
        assertThat(true, is(matched(regex, "abc1111")));
 
        assertThat(false, is(matched(regex, "abc11")));
        assertThat(false, is(matched(regex, "ab11")));
        assertThat(false, is(matched(regex, "ab111111")));
    }
 
    /**
     * <pre>
     * or연산1 |
     *
     * <pre>
     */
    @Test
    public void or연산() {
        String regex = "hello|world";
        assertThat(true, is(matched(regex, "hello")));
        assertThat(true, is(matched(regex, "world")));
        assertThat(false, is(matched(regex, "helloworld")));
        assertThat(false, is(matched(regex, "hello|world")));
 
        String regex1 = "(hello|world){1}";
        assertThat(true, is(matched(regex1, "hello")));
        assertThat(true, is(matched(regex1, "world")));
        assertThat(false, is(matched(regex1, "helloworld")));
    }
 
    /**
     * <pre>
     * 알파벳이나숫자 \w
     *
     * <pre>
     */
    @Test
    public void 알파벳이나숫자() {
        String regex = "\\w";
        assertThat(true, is(matched(regex, "a")));
        assertThat(true, is(matched(regex, "Z")));
        assertThat(true, is(matched(regex, "4")));
 
        assertThat(false, is(matched(regex, "$")));
        assertThat(false, is(matched(regex, "남")));
        assertThat(false, is(matched(regex, "12")));
    }
 
    /**
     * <pre>
     * 알파벳이나숫자를제외한문자 \W
     *
     * <pre>
     */
    @Test
    public void 알파벳이나숫자를제외한문자() {
        String regex = "\\W";
        assertThat(true, is(matched(regex, "@")));
        assertThat(true, is(matched(regex, ")")));
        assertThat(true, is(matched(regex, "\\")));
        assertThat(false, is(matched(regex, "1")));
        assertThat(false, is(matched(regex, "a")));
 
        String regex1 = "\\W{3}";
        assertThat(true, is(matched(regex1, "!@#")));
        assertThat(true, is(matched(regex1, ")(*")));
    }
 
    /**
     * <pre>
     * 숫자만 \d
     *
     * <pre>
     */
    @Test
    public void 숫자만() {
        String regex = "\\d";
        assertThat(true, is(matched(regex, "1")));
        assertThat(true, is(matched(regex, "3")));
        assertThat(true, is(matched(regex, "4")));
 
        assertThat(false, is(matched(regex, "a")));
        assertThat(false, is(matched(regex, "Z")));
        assertThat(false, is(matched(regex, "11")));
    }
 
    /**
     * <pre>
     * 숫자를제외한모든문자 \\D
     *
     * <pre>
     */
    @Test
    public void 숫자를제외한모든문자() {
        String regex = "\\D";
        assertThat(true, is(matched(regex, "a")));
        assertThat(true, is(matched(regex, "한")));
        assertThat(true, is(matched(regex, "규")));
 
        assertThat(false, is(matched(regex, "1")));
        assertThat(false, is(matched(regex, "3")));
        assertThat(false, is(matched(regex, "44")));
    }
 
    /**
     * <pre>
     * 역슬래스테스트 \
     *
     * <pre>
     */
    @Test
    public void 역슬래스테스트() {
        String regex = "a\\sb";
        assertThat(true, is(matched(regex, "a b")));
 
        String regex1 = "a\\tb";
        assertThat(true, is(matched(regex1, "a    b")));
 
        String regex2 = "a\\nb";
        assertThat(true, is(matched(regex2, "a\nb")));
    }
 
    /**
     * <pre>
     * 대소문자구분안함 (?i)
     *
     * <pre>
     */
    @Test
    public void 대소문자구분안함() {
        String regex = "(?i)abc";
        assertThat(true, is(matched(regex, "abc")));
        assertThat(true, is(matched(regex, "ABC")));
        assertThat(true, is(matched(regex, "aBc")));
 
        String regex1 = "abc";
        assertThat(false, is(matched(regex1, "ABC")));
        assertThat(false, is(matched(regex1, "abC")));
        assertThat(false, is(matched(regex1, "aBc")));
    }
 
    @Test
    public void 특수문자테스트() {
        String regex = "^[a-z0-9A-Z]*@#";
        assertThat(true, is(matched(regex, "12@#")));
    }
 
    /**
     * <pre>
     * 영어숫자만
     *
     * <pre>
     */
    @Test
    public void 영어숫자만() {
        String regex = "^[0-9a-zA-Z]*$";
        assertThat(true, is(matched(regex, "1212dafda")));
        assertThat(true, is(matched(regex, "1")));
        assertThat(true, is(matched(regex, "a")));
        assertThat(true, is(matched(regex, "Z")));
        assertThat(true, is(matched(regex, "")));
 
        assertThat(false, is(matched(regex, "한")));
    }
 
    /**
     * <pre>
     * 한글만
     *
     * <pre>
     */
    @Test
    public void 한글만() {
        String regex = "^[가-힣]*$";
        assertThat(true, is(matched(regex, "가")));
        assertThat(true, is(matched(regex, "가나다")));
        assertThat(true, is(matched(regex, "가나다라하햐호호유")));
        assertThat(true, is(matched(regex, "뷁")));
        assertThat(true, is(matched(regex, "힔")));
 
        assertThat(false, is(matched(regex, "1")));
        assertThat(false, is(matched(regex, "a")));
        assertThat(false, is(matched(regex, "Z")));
    }
 
    /**
     * <pre>
     * replaceAllTest
     * 한글 삭제
     * <pre>
     */
    @Test
    public void replaceAllTest() {
        String value = "abc한123";
        String hangulDeleteValue = value.replaceAll("[가-힣]*""");
        assertThat(true, is(matched(hangulDeleteValue, "abc123")));
    }
 
    /**
     * <pre>
     * 이메일체크
     *
     * <pre>
     */
    @Test
    public void 이메일체크() {
        String emailRegex = "^[a-z0-9A-Z_-]*@[a-z0-9A-Z]*.[a-zA-Z.]*$";
        assertThat(true, is(matched(emailRegex, "lng1982@naver.com")));
        assertThat(true, is(matched(emailRegex, "lng1982@naver.co.kr")));
        assertThat(true, is(matched(emailRegex, "lng1982@daum.net")));
        assertThat(true, is(matched(emailRegex, "lng1982@test.org")));
 
        assertThat(false, is(matched(emailRegex, "lng1982$@naver.com")));
        assertThat(false, is(matched(emailRegex, "lng1982naver.com")));
        assertThat(false, is(matched(emailRegex, "lng1982naver.com12")));
    }
 
    /**
     * <pre>
     * 휴대폰체크
     *
     * <pre>
     */
    @Test
    public void 휴대폰체크() {
        String phoneRegex = "^0\\d{2}";
        assertThat(true, is(matched(phoneRegex, "010")));
        assertThat(true, is(matched(phoneRegex, "011")));
        assertThat(true, is(matched(phoneRegex, "019")));
        assertThat(false, is(matched(phoneRegex, "111")));
        assertThat(false, is(matched(phoneRegex, "11a")));
 
        String phoneRegex1 = "^0\\d{2}\\d{3,4}";
        assertThat(true, is(matched(phoneRegex1, "010866")));
        assertThat(true, is(matched(phoneRegex1, "0108667")));
        assertThat(false, is(matched(phoneRegex1, "01086677")));
        assertThat(false, is(matched(phoneRegex1, "010866771")));
 
        String phoneRegex2 = "^0\\d{2}\\d{3,4}\\d{4}$";
        assertThat(true, is(matched(phoneRegex2, "01086677100")));
        assertThat(true, is(matched(phoneRegex2, "01086677101")));
        assertThat(true, is(matched(phoneRegex2, "0108667710")));
        assertThat(false, is(matched(phoneRegex2, "010866771000")));
        assertThat(false, is(matched(phoneRegex2, "01086677100a")));
        assertThat(false, is(matched(phoneRegex2, "11086677100a")));
    }
 
    /**
     * <pre>
     * 주민등록번호
     *
     * <pre>
     */
    @Test
    public void 주민등록번호() {
        String regex = "^\\d{6}(1|2|3|4)";
        assertThat(true, is(matched(regex, "8210111")));
        assertThat(true, is(matched(regex, "8210112")));
        assertThat(true, is(matched(regex, "8210113")));
        assertThat(true, is(matched(regex, "8210114")));
        assertThat(false, is(matched(regex, "8210115")));
        assertThat(false, is(matched(regex, "82101151")));
 
        String regex1 = "^\\d{6}(1|2|3|4)\\d{6}$";
        assertThat(true, is(matched(regex1, "8210201111111")));
        assertThat(true, is(matched(regex1, "8210202222222")));
        assertThat(true, is(matched(regex1, "8210203333333")));
        assertThat(true, is(matched(regex1, "8210204444444")));
        assertThat(false, is(matched(regex1, "8210205555555")));
        assertThat(false, is(matched(regex1, "82102055555551")));
        assertThat(false, is(matched(regex1, "8210205a55551")));
        assertThat(false, is(matched(regex1, "8210205\55551")));
 
        String regex2 = "^\\d{6}[1-4]\\d{6}$";
        assertThat(true, is(matched(regex2, "8210201111111")));
        assertThat(true, is(matched(regex2, "8210202111111")));
        assertThat(true, is(matched(regex2, "8210203111111")));
        assertThat(true, is(matched(regex2, "8210204111111")));
    }
 
    /**
     * <pre>
     * 아이피체크
     *
     * <pre>
     */
    @Test
    public void 아이피체크() {
        String regex = "^\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}$";
        assertThat(true, is(matched(regex, "172.18.167.92")));
        assertThat(true, is(matched(regex, "172.181.167.921")));
        assertThat(true, is(matched(regex, "1.181.167.921")));
        assertThat(true, is(matched(regex, "1.1.167.921")));
        assertThat(true, is(matched(regex, "1.1.7.921")));
        assertThat(true, is(matched(regex, "1.1.7.9")));
 
        assertThat(false, is(matched(regex, "1721.181.167.921")));
        assertThat(false, is(matched(regex, "172.1811.167.921")));
        assertThat(false, is(matched(regex, "172.181.1671.921")));
        assertThat(false, is(matched(regex, "172.181.167.9211")));
    }
 
    /**
     * <pre>
     * 파일확장자체크
     *
     * <pre>
     */
    @Test
    public void 파일확장자체크() {
        String regex = "^\\S+.(?i)(png|jpg|bmp|gif)$";
        assertThat(true, is(matched(regex, "test.png")));
        assertThat(true, is(matched(regex, "test.jpg")));
        assertThat(true, is(matched(regex, "test.bmp")));
        assertThat(true, is(matched(regex, "test.gif")));
        assertThat(true, is(matched(regex, "test1#$adf한.gif")));
        assertThat(true, is(matched(regex, "test.GIF")));
        assertThat(true, is(matched(regex, "test.JPG")));
        assertThat(true, is(matched(regex, "test.BMP")));
        assertThat(true, is(matched(regex, "test.PNG")));
        assertThat(true, is(matched(regex, "testA.PNG")));
 
        assertThat(false, is(matched(regex, "test a.gif")));
        assertThat(false, is(matched(regex, "test a.jpg")));
 
 
    }
 
    @Test
    public void 비밀번호체크() {
        String regex = "^[a-zA-Z0-9~!@#$%^&*()]{8,16}";
        assertThat(true, is(matched(regex, "adfadf1212")));
        assertThat(true, is(matched(regex, "a23d3f31")));
        assertThat(true, is(matched(regex, "rbdn200111111111")));
        assertThat(true, is(matched(regex, "aaaaaaaaaa212121")));
        assertThat(true, is(matched(regex, "aBdAf1234")));
        assertThat(true, is(matched(regex, "abc~!@#$234")));
        assertThat(true, is(matched(regex, "~!@#$%^&*()")));
        assertThat(true, is(matched(regex, "1asfdA~!@af^&*()")));
 
        assertThat(false, is(matched(regex, "a")));
        assertThat(false, is(matched(regex, "a121212")));
        assertThat(false, is(matched(regex, "a121212a121212a121212")));
    }
 
    @Test
    public void 비밀번호체크2() {
        String regex = "^([a-zA-Z]+[0-9]+[~!@#$%^&*()]+)";
        assertThat(true, is(matched(regex, "a0@")));
        assertThat(false, is(matched(regex, "a@")));
        assertThat(false, is(matched(regex, "@1")));
    }
    
    /**
     * <pre>
     * groupingTest
     * hello kyu world 문자열을 hello namkyu world 로 바꾸기
     * 간단하게 hello와 world를 그룹으로 지정하기 위해서 괄호로 묶어서 아래와 같이 처리
     * <pre>
     */
    @Test
    public void groupingTest() {
        String result = "hello kyu world".replaceAll("(hello) kyu (world)""$1 namkyu $2");
        assertThat("hello namkyu world", is(result));
    }
 
    /**
     * <pre>
     * nonGroupingTest
     * ?: <-- 이 문자열이 non grouping 문자
     * hello kyu world 문자열에서 양쪽의 hello, world를 삭제한 kyu만 출력
     * <pre>
     */
    @Test
    public void nonGroupingTest() {
        String result = "hello kyu world".replaceAll("(?:hello) (kyu) (?:world)""$1");
        assertThat("kyu", is(result));
    }
 
    /**
     * <pre>
     * matched
     *
     * <pre>
     * @param regex
     * @param inputTxt
     * @return
     */
    private boolean matched(String regex, String inputTxt) {
        return Pattern.matches(regex, inputTxt);
    }
}
cs


Comments