Both sides previous revisionPrevious revisionNext revision | Previous revision |
regular_expression [2022/10/16 14:14] – 문단 정리 ledyx | regular_expression [2022/10/16 16:21] (current) – 문단 수정 ledyx |
---|
== Lookaround == | == Lookaround == |
| |
일치하는 영역을 제외하고, 일치하는 영역 기준으로 이전 혹은 이후 값을 반환 | 일치하는 영역을 제외하고, 일치하는 영역 기준으로 이전 혹은 이후 값을 반환. |
| |
=== Lookahead === | 부정형은 "<nowiki>=</nowiki>" 대신 "!"을 대체한다. |
| |
전방 탐색 | |<100%>| |
| ^ Metacharacter ^ 설명 ^ |
| | <nowiki>(?=)</nowiki> | 긍정형 전방탐색 (Positive Lookahead) | |
| | <nowiki>(?<=)</nowiki> | 긍정형 후방탐색 (Positive Lookbehind) | |
| |
* Positive : <nowiki>?=</nowiki> | ^ Metacharacter ^ 설명 ^ |
* Negative : <nowiki>?!</nowiki> | | <nowiki>(?!)</nowiki> | 부정형 전방탐색 (Negative Lookahead) | |
| | <nowiki>(?<!)</nowiki> | 부정형 후방탐색 (Negative Lookbehind) | |
| |
| === 예시 === |
| |
| * 긍정형 전방탐색 |
| |
<code> | <code> |
<bg skyblue>https</bg><nowiki>://mail.google.com</nowiki> \\ | <bg skyblue>https</bg><nowiki>://mail.google.com</nowiki> \\ |
<bg skyblue>ftp</bg><nowiki>://ftp.xxx.xyz</nowiki> \\ | <bg skyblue>ftp</bg><nowiki>://ftp.xxx.xyz</nowiki> \\ |
| |
| * 긍정형 후방탐색 |
| |
| <code> |
| (?<=\$)[0-9.]+ |
| </code> |
| |
| A11 : $<bg skyblue>23.34</bg> \\ |
| B22 : $<bg skyblue>5.31</bg> \\ |
| Total items found : 2 |
| |
| * 부정형 전후방탐색 |
| |
| <code> |
| \b(?<!\$)\d+\b |
| </code> |
| |
| I paid $30 for <bg skyblue>100</bg> apples, |
| <bg skyblue>50</bg> oranges, and <bg skyblue>60</bg> pears. |
| I saved $5 on this order. |
| |
| |
| |
| |
=== Lookbehind === | |
후방 탐색. | |
| |
* Positive : <nowiki>?<=</nowiki> | = 언어별 활용 = |
* Negative : <nowiki>?<!</nowiki> | |
| |
<code> | == Java == |
(?<=\$)[0-9.]+ | |
</code> | |
| |
A11 : $<bg skyblue>23.34</bg> \\ | |
B22 : $<bg skyblue>5.31</bg> \\ | |
Total items found : 2 | |
| |
| |
= Java = | |
| |
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html | https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html |
</sxh> | </sxh> |
| |
== Pattern Constant == | === Pattern Constant === |
| |
<sxh> | <sxh> |
</sxh> | </sxh> |
| |
== POSIX character classes (US-ASCII only) == | === POSIX character classes (US-ASCII only) === |
| |
<sxh> | <sxh> |
\p{XDigit} A hexadecimal digit: [0-9a-fA-F] | \p{XDigit} A hexadecimal digit: [0-9a-fA-F] |
\p{Space} A whitespace character: [ \t\n\x0B\f\r] | \p{Space} A whitespace character: [ \t\n\x0B\f\r] |
</sxh> | |
= 활용 = | |
| |
== 전화번호 == | |
<sxh java> | |
^(02|0[3-6]{1}[1-5]{1})-?[0-9]{3,4}-?[0-9]{4}$ //지역번호-xxx(x)-xxxx | |
^(15(44|77|88|99)|1644)-?[0-9]{4}$ //15xx/1644-xxxx | |
</sxh> | |
| |
== 소괄호 안 문자(= Parameter) 추출 == | |
<sxh java> | |
String str = "(int a, int b)"; | |
| |
Pattern p = Pattern.compile("\\((.*?)\\)"); | |
Matcher m = p.matcher(str); | |
| |
while(m.find()) | |
System.out.println(m.group(1)); | |
</sxh> | </sxh> |
| |