This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| regular_expression [2015/04/03 09:25] – ledyx | regular_expression [2022/10/16 15:21] (current) – 문단 수정 ledyx | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | = 정규식 = | + | = Regular Expression = |
| + | {{tag> | ||
| + | |||
| + | |||
| + | 일부 내용은 [[https:// | ||
| + | |||
| + | = 기본 개념 = | ||
| + | |||
| + | == Basic Metacharacters == | ||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | | ||
| + | | | ||
| + | | | ||
| + | | ? | ||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | < | ||
| + | | < | ||
| + | |||
| + | == 범위 == | ||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | < | ||
| + | | < | ||
| + | | | ||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | < | ||
| + | | < | ||
| + | | < | ||
| + | | < | ||
| + | | < | ||
| + | | < | ||
| + | |||
| + | == 공백 문자 == | ||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | | ||
| + | |||
| + | ^ Metacharacter | ||
| + | | | ||
| + | | | ||
| + | | | ||
| + | | | ||
| + | | | ||
| + | |||
| + | ^ Metacharacter | ||
| + | | | ||
| + | | | ||
| + | |||
| + | == Quantifier == | ||
| + | |||
| + | 수량자. {// | ||
| + | |||
| + | 과하게 일치하는 상황 방지 | ||
| + | |||
| + | * Greedy Qualifier : 최대로 일치 | ||
| + | * Lazy Qualifier : 최소로 일치 | ||
| + | |||
| + | |< | ||
| + | ^ Greedy Qualifier | ||
| + | | | ||
| + | | | ||
| + | | | ||
| + | |||
| + | |||
| + | === 예시 === | ||
| + | |||
| + | < | ||
| + | (? | ||
| + | </ | ||
| + | <bg skyblue>< | ||
| + | |||
| + | |||
| + | < | ||
| + | (? | ||
| + | </ | ||
| + | <bg skyblue>< | ||
| + | |||
| + | |||
| + | == 위치 지정 == | ||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | < | ||
| + | | < | ||
| + | | < | ||
| + | |||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | | ||
| + | | | ||
| + | |||
| + | |||
| + | == Lookaround == | ||
| + | |||
| + | 일치하는 영역을 제외하고, | ||
| + | |||
| + | 부정형은 "< | ||
| + | |||
| + | |< | ||
| + | ^ Metacharacter | ||
| + | | < | ||
| + | | < | ||
| + | |||
| + | ^ Metacharacter | ||
| + | | < | ||
| + | | < | ||
| + | |||
| + | === 예시 === | ||
| + | |||
| + | * 긍정형 전방탐색 | ||
| + | |||
| + | < | ||
| + | .+(?=:) | ||
| + | </ | ||
| + | |||
| + | <bg skyblue> | ||
| + | <bg skyblue> | ||
| + | <bg skyblue> | ||
| + | |||
| + | * 긍정형 후방탐색 | ||
| + | |||
| + | < | ||
| + | (?< | ||
| + | </ | ||
| + | |||
| + | A11 : $<bg skyblue> | ||
| + | B22 : $<bg skyblue> | ||
| + | Total items found : 2 | ||
| + | |||
| + | * 부정형 전후방탐색 | ||
| + | |||
| + | < | ||
| + | \b(?< | ||
| + | </ | ||
| + | |||
| + | I paid $30 for <bg skyblue> | ||
| + | <bg skyblue> | ||
| + | I saved $5 on this order. | ||
| + | |||
| + | |||
| + | == Backreference == | ||
| + | |||
| + | **괄호로 감싼 하위 표현식**을 참조하는 정규 표현식. $1~n 으로 표현. (일부 언어는 $대신 /을 사용하기도 한다.) | ||
| + | |||
| + | 변수와 비슷. | ||
| + | |||
| + | === 예시 === | ||
| - | Java에서 아래와 같은 구문으로 이용한다. | ||
| <sxh java> | <sxh java> | ||
| - | Pattern p = Pattern.compile("^REGULAR_EXPRESSION$"); | + | String eg1 = " |
| - | Matcher m = p.matcher(STRING); | + | String result1 = eg1.replaceAll("(\\w+[\\w.]*@[\\w.]+\\.\\w+)", |
| - | boolean b = m.matches(); | + | System.out.println(result1); // Hello, <a href=" |
| + | |||
| + | String eg2 = " | ||
| + | String result2 = eg2.replaceAll(" | ||
| + | System.out.println(result2); | ||
| </ | </ | ||
| - | = 전화번호 | + | |
| + | |||
| + | = 언어별 활용 | ||
| + | |||
| + | == Java == | ||
| + | |||
| + | https:// | ||
| <sxh java> | <sxh java> | ||
| - | ^(02|0[3-6]{1}[1-5]{1})-? | + | Pattern p = Pattern.compile(" |
| - | ^(15(44|77|88|99)|1644)-? | + | Matcher m = p.matcher(STRING); |
| + | boolean b = m.matches(); | ||
| </ | </ | ||
| - | = 소괄호 안 문자(= Parameter) 추출 | + | === Pattern Constant === |
| - | <sxh java> | + | |
| - | String str = "(int a, int b)"; | + | |
| - | Pattern | + | < |
| - | Matcher m = p.matcher(str); | + | Pattern.CASE_INSENSITIVE |
| + | Pattern.COMMENTS | ||
| + | Pattern.MULTILINE | ||
| + | Pattern.DOTALL | ||
| + | Pattern.LITERAL | ||
| + | Pattern.UNICODE_CASE | ||
| + | Pattern.UNIX_LINES | ||
| + | </ | ||
| - | while(m.find()) | + | === POSIX character classes |
| - | System.out.println(m.group(1)); | + | |
| + | < | ||
| + | \p{Lower} A lower-case alphabetic character: [a-z] | ||
| + | \p{Upper} An upper-case alphabetic character: | ||
| + | \p{ASCII} All ASCII: | ||
| + | \p{Alpha} An alphabetic character: | ||
| + | \p{Digit} A decimal digit: [0-9] | ||
| + | \p{Alnum} An alphanumeric character: | ||
| + | \p{Punct} Punctuation: | ||
| + | \p{Graph} A visible character: [\p{Alnum}\p{Punct}] | ||
| + | \p{Print} A printable character: [\p{Graph}\x20] | ||
| + | \p{Blank} A space or a tab: [ \t] | ||
| + | \p{Cntrl} A control character: [\x00-\x1F\x7F] | ||
| + | \p{XDigit} A hexadecimal digit: [0-9a-fA-F] | ||
| + | \p{Space} A whitespace character: [ \t\n\x0B\f\r] | ||
| </ | </ | ||
| + | |||