Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
regular_expression [2022/10/16 06:47] – Lookaround ledyxregular_expression [2022/10/16 16:21] (current) – 문단 수정 ledyx
Line 6: Line 6:
  
 = 기본 개념 = = 기본 개념 =
 +
 +== Basic Metacharacters ==
 +
 +|<100%>|
 +^  Metacharacter  ^  설명  ^  비고  ^
 +|      |   문자 1개 일치                |            |
 +|       문자 1개 이상 일치        |  {1,} 와 같음   |
 +|       문자 0개 이상 일치 \\ (문자가 없거나 여러 개 일치)        {0,} 와 같음  |
 +|   ?     문자 0개 혹은 1개 일치 \\ (문자가 없거나 1개 일치)  |  {0,1} 와 같음  |
 +
 +|<100%>|
 +^  Metacharacter  ^  설명  ^
 +|   <nowiki>|</nowiki>    |   OR  |
 +|   <nowiki>\</nowiki>    |   Metacharacter를 일반 문자로 인식  |
 +
 +== 범위 ==
 +
 +|<100%>|
 +^  Metacharacter  ^  설명  ^  비고  ^
 +|   <nowiki>[]</nowiki>  |   문자 집합 구성원 중 하나와 일치          AND가 아닌 __**OR**__ 조건이다!  |
 +|   <nowiki>[^]</nowiki>     문자 집합 구성원을 제외하고 일치           |
 +|       범위 정의  ||
 +
 +|<100%>|
 +^  Metacharacter  ^  설명  ^  비고  ^
 +|   <nowiki>\d</nowiki>  |  10진수 숫자 일치  |  <nowiki>[0-9]</nowiki> 와 같음  |
 +|   <nowiki>\D</nowiki>  |  \d와 반대로 일치  |  <nowiki>[^0-9]</nowiki> 와 같음  |
 +|   <nowiki>\x</nowiki>  |  16진수 숫자 일치  |  <nowiki>[0-9a-fA-F]</nowiki> 와 같음  |
 +|   <nowiki>\0</nowiki>  |  8진수 숫자 일치  ||
 +|   <nowiki>\w</nowiki>  |  영문, 숫자, _ 일치  |  <nowiki>[a-zA-Z0-9_]</nowiki> 와 같음  |
 +|   <nowiki>\W</nowiki>    \w와 반대로 일치  |  <nowiki>[^a-zA-Z0-9_]</nowiki> 와 같음  |
 +
 +== 공백 문자 ==
 +
 +|<100%>|
 +^  Metacharacter  ^  설명  ^
 +|   [\b]    |   Backspace  |
 +
 +^  Metacharacter  ^  설명  ^
 +|   \f    |   페이지 넘김(form feed)  |
 +|   \n    |   줄바꿈 (new line)  |
 +|   \r    |   Carrige Retrun (커서/캐럿을 그 줄의 맨 앞으로 이동)  |
 +|   \t    |   탭  |
 +|   \v    |   수직 탭  |
 +
 +^  Metacharacter  ^  설명  ^  비고  ^
 +|   \s    |   모든 공백 문자와 일치 \\  |  [\f\n\r\t\v] 와 같다  |
 +|   \S    |   \s와 반대로 일치  |  <nowiki>[^\f\n\r\t\v]</nowiki> 와 같다  |
  
 == Quantifier == == Quantifier ==
  
-수량자.+수량자. {//MIN//,//MAX//} (//MAX//는 생략 가능)
  
 과하게 일치하는 상황 방지 과하게 일치하는 상황 방지
Line 37: Line 85:
  
  
-== Backreference ==+==  위치 지정 ==
  
-**괄호로 감싼 하위 표현식**을 참조하는 정규 표현식. $1~n 으로 표현. (일부 언어는 $대신 /을 사용하기도 한다.)+|<100%>
 +^  Metacharacter  ^  설명  ^  비고 
 +|   <nowiki>^</nowiki>  |   전체 문자열의 시작과 일치  |  일부 언어는 <nowiki>\A</nowiki>Java, JavaScript는 미지원 
 +|   <nowiki>$</nowiki>     전체 문자열의 끝과 일치    |  일부 언어는 <nowiki>\Z</nowiki>Java, JavaScript는 미지원 
 +|   <nowiki>(?m)</nowiki>     다중행 모드. <nowiki>^</nowiki>와 <nowiki>$</nowiki>를 행별로 인식    |  일부 언어는 미지원. Java, Javascript는 지원  |
  
-변수와 비슷. 
  
-=== 예시 ===+|<100%>
 +^  Metacharacter  ^  설명  ^  비고 
 +|   \b  |   단어 경계와 일치  |   **b**oundary | 
 +|   \B  |   \b 반대로 일치  ||
  
-<sxh java> 
-String eg1 = "Hello, xxx@xxx.com is my email address."; 
-String result1 = eg1.replaceAll("(\\w+[\\w.]*@[\\w.]+\\.\\w+)", "<a href=\"$1\">$1</href>"); 
-System.out.println(result1); // Hello, <a href="ben@forta.com">ben@forta.com</href> is my email address. 
  
-String eg2 "031-123-1234"; +== Lookaround ==
-String result2 eg2.replaceAll("(\\d{3})(-)(\\d{3})(-)(\\d{4})", "($1) $3-$5"); +
-System.out.println(result2); // (031) 123-1234 +
-</sxh>+
  
 +일치하는 영역을 제외하고, 일치하는 영역 기준으로 이전 혹은 이후 값을 반환.
  
-== Lookaround ==+부정형은 "<nowiki>=</nowiki>" 대신 "!"을 대체한다.
  
-일치하는 영역을 제외하고, 일치하는 영역 기준으로 이전 혹은 이후 값을 반환+|<100%>
 +^  Metacharacter  ^  설명 
 +|   <nowiki>(?=)</nowiki>  |   긍정형 방탐색 (Positive Lookahead) 
 +|   <nowiki>(?<=)</nowiki>  |   긍정형 방탐색 (Positive Lookbehind)  |
  
-=== Lookahead ===+^  Metacharacter  ^  설명 
 +|   <nowiki>(?!)</nowiki>  |   부정형 전방탐색 (Negative Lookahead)  | 
 +|   <nowiki>(?<!)</nowiki>  |   부정형 후방탐색 (Negative Lookbehind)  |
  
-전방 탐색.  <nowiki>?=</nowiki>+=== 예시 ===
  
 +  * 긍정형 전방탐색
  
 <code> <code>
Line 73: Line 127:
 <bg skyblue>ftp</bg><nowiki>://ftp.xxx.xyz</nowiki> \\ <bg skyblue>ftp</bg><nowiki>://ftp.xxx.xyz</nowiki> \\
  
- +  * 긍정형 후방탐색
-===  Lookbehind  === +
-후방 탐색.  <nowiki>?<=</nowiki> +
  
 <code> <code>
Line 86: Line 137:
 Total items found : 2 Total items found : 2
  
 +  * 부정형 전후방탐색
  
-= Java =+<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. 
 + 
 + 
 +== Backreference == 
 + 
 +**괄호로 감싼 하위 표현식**을 참조하는 정규 표현식. $1~n 으로 표현. (일부 언어는 $대신 /을 사용하기도 한다.) 
 + 
 +변수와 비슷. 
 + 
 +=== 예시 === 
 + 
 +<sxh java> 
 +String eg1 = "Hello, xxx@xxx.com is my email address."; 
 +String result1 = eg1.replaceAll("(\\w+[\\w.]*@[\\w.]+\\.\\w+)", "<a href=\"$1\">$1</href>"); 
 +System.out.println(result1); // Hello, <a href="ben@forta.com">ben@forta.com</href> is my email address. 
 + 
 +String eg2 = "031-123-1234"; 
 +String result2 = eg2.replaceAll("(\\d{3})(-)(\\d{3})(-)(\\d{4})", "($1) $3-$5"); 
 +System.out.println(result2); // (031) 123-1234 
 +</sxh> 
 + 
 + 
 + 
 += 언어별 활용 = 
 + 
 +== 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
Line 97: Line 180:
 </sxh> </sxh>
  
-== Pattern Constant ==+=== Pattern Constant ===
  
 <sxh> <sxh>
Line 109: Line 192:
 </sxh> </sxh>
  
-== POSIX character classes (US-ASCII only) ==+=== POSIX character classes (US-ASCII only) ===
  
 <sxh> <sxh>
Line 126: Line 209:
 \p{Space} A whitespace character: [ \t\n\x0B\f\r] \p{Space} A whitespace character: [ \t\n\x0B\f\r]
 </sxh> </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> 
regular_expression.1665899230.txt.gz · Last modified: 2022/10/16 06:47 by ledyx