sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

1. API 내용
boolean java.util.regex.Pattern.matches(String regex, CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.

An invocation of this convenience method of the form

 Pattern.matches(regex, input);
behaves in exactly the same way as the expression
 Pattern.compile(regex).matcher(input).matches()
If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.

Parameters:
regex The expression to be compiled
input The character sequence to be matched
Throws:
PatternSyntaxException If the expression's syntax is invalid

boolean java.util.regex.Matcher.matches()
Attempts to match the entire region against the pattern.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, the entire region sequence matches this matcher's pattern

boolean java.util.regex.Matcher.find()
Attempts to find the next subsequence of the input sequence that matches the pattern.

This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, a subsequence of the input sequence matches this matcher's pattern



대충 해석해 보면 Pattern.matches() 는 Matcher.matches() 와 같은 코드이고, Matcher.find() 와 구별되는 점은 다음과 같다.

matches() 함수는 입력된 문자열 전체에 대해서 매칭 검사를 실시하고, find() 함수는 입력된 문자열을 순회하면서 매칭 검사를 실시한다.

그말인 즉슨, matches() 함수를 쓰면 "abcd" 라는 문자열을 검사할때 "adcd" 가 모두 검사될 수 있도록 "abcd"라고 정규식을 작성해야지만

true 를 리턴하지만 find() 함수는 "abcd" 에서 "abc" 로 정규식을 작성해도 true 를 리턴할 수 있다는 야그다.

만일 matches() 에서 "abc" 로 정규식을 작성하면 false 를 리턴한다. "abc"를 찾아라가 아니라 "abc" 형태가 맞느냐? 라고 찾기 때문이다.

이를 비교한 샘플 프로그램이다.


 

·미리보기 | 소스복사·
  1. // -----------------------------------------------------------------------------   
  2. // RegDemoSun.java   
  3. // -----------------------------------------------------------------------------   
  4.   
  5. import java.util.regex.*;   
  6.   
  7. /**  
  8.  * -----------------------------------------------------------------------------  
  9.  * Used to provide an example of how to utilize the Java 2 Regular Expression  
  10.  * Implementation from Sun. It is important to note that a Regular Expression  
  11.  * implentation didn't appear in the Java SDK until version 1.4.  
  12.  *   
  13.  * Sun's implementation of its regular expression parser enables you to both  
  14.  * "find" and "match" character sequences against a particular pattern. Using  
  15.  * "find" enables the user to find matches in a string while "match" requires  
  16.  * an EXACT match of a regular expression.  
  17.  * -----------------------------------------------------------------------------  
  18.  */  
  19.     
  20. public class RegDemoSun {   
  21.   
  22.     private static void doRegDemo() {   
  23.   
  24.         String patternStrFull    = "^A[^b]-d+ - .+$";   
  25.         String patternStrPart    = "^A[^b]-d+ - ";   
  26.   
  27.         CharSequence inputStr1      = "AU-120 - Network Cable.";   
  28.         CharSequence inputStr2      = "au-120 - Network Cable.";   
  29.   
  30.         Pattern patternFull         = Pattern.compile(patternStrFull);   
  31.         Pattern patternPart         = Pattern.compile(patternStrPart);   
  32.         Pattern patternCaseInsFull  = Pattern.compile(patternStrFull, Pattern.CASE_INSENSITIVE);   
  33.         Pattern patternCaseInsPart  = Pattern.compile(patternStrPart, Pattern.CASE_INSENSITIVE);   
  34.            
  35.         Matcher matcher = null;   
  36.   
  37.         String matchStr     = " [ MATCHES ]  ";   
  38.         String noMatchStr   = " [ DOES NOT MATCH ]  ";   
  39.         String foundStr     = " [ FOUND ]  ";   
  40.         String noFoundStr   = " [ NOT FOUND ]  ";   
  41.         boolean found   = false;   
  42.            
  43.         System.out.println();   
  44.   
  45.   
  46.         // -------------------------------------------------------   
  47.         // Test 1 : (Case Sensitive / match() v.s. find() )   
  48.         // -------------------------------------------------------   
  49.   
  50.         System.out.println("Case sensitive :: matches() v.s. find()");   
  51.         System.out.println("---------------------------------------n");   
  52.            
  53.         matcher  = patternFull.matcher(inputStr1);   
  54.         found    = matcher.matches();   
  55.         System.out.println("Test 1: /" + inputStr1 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");   
  56.   
  57.         matcher  = patternPart.matcher(inputStr1);   
  58.         found    = matcher.matches();   
  59.         System.out.println("Test 2: /" + inputStr1 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");   
  60.   
  61.         matcher = patternFull.matcher(inputStr1);   
  62.         found = matcher.find();   
  63.         System.out.println("Test 3: /" + inputStr1 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");   
  64.   
  65.         matcher = patternPart.matcher(inputStr1);   
  66.         found = matcher.find();   
  67.         System.out.println("Test 4: /" + inputStr1 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");   
  68.   
  69.         matcher  = patternFull.matcher(inputStr2);   
  70.         found    = matcher.matches();   
  71.         System.out.println("Test 5: /" + inputStr2 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");   
  72.   
  73.         matcher  = patternPart.matcher(inputStr2);   
  74.         found    = matcher.matches();   
  75.         System.out.println("Test 6: /" + inputStr2 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");   
  76.   
  77.         matcher = patternFull.matcher(inputStr2);   
  78.         found = matcher.find();   
  79.         System.out.println("Test 7: /" + inputStr2 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");   
  80.   
  81.         matcher = patternPart.matcher(inputStr2);   
  82.         found = matcher.find();   
  83.         System.out.println("Test 8: /" + inputStr2 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");   
  84.   
  85.         System.out.println();   
  86.   
  87.   
  88.   
  89.         // -------------------------------------------------------   
  90.         // Test 2 : (Case Insensitive / match() v.s. find() )   
  91.         // -------------------------------------------------------   
  92.            
  93.         System.out.println("Case Insensitive :: matches() v.s. find()");   
  94.         System.out.println("-----------------------------------------n");   
  95.            
  96.         matcher  = patternCaseInsFull.matcher(inputStr1);   
  97.         found    = matcher.matches();   
  98.         System.out.println("Test 1: /" + inputStr1 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");   
  99.   
  100.         matcher  = patternCaseInsPart.matcher(inputStr1);   
  101.         found    = matcher.matches();   
  102.         System.out.println("Test 2: /" + inputStr1 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");   
  103.   
  104.         matcher = patternCaseInsFull.matcher(inputStr1);   
  105.         found = matcher.find();   
  106.         System.out.println("Test 3: /" + inputStr1 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");   
  107.   
  108.         matcher = patternCaseInsPart.matcher(inputStr1);   
  109.         found = matcher.find();   
  110.         System.out.println("Test 4: /" + inputStr1 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");   
  111.   
  112.         matcher  = patternCaseInsFull.matcher(inputStr2);   
  113.         found    = matcher.matches();   
  114.         System.out.println("Test 5: /" + inputStr2 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");   
  115.   
  116.         matcher  = patternCaseInsPart.matcher(inputStr2);   
  117.         found    = matcher.matches();   
  118.         System.out.println("Test 6: /" + inputStr2 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");   
  119.   
  120.         matcher = patternCaseInsFull.matcher(inputStr2);   
  121.         found = matcher.find();   
  122.         System.out.println("Test 7: /" + inputStr2 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");   
  123.   
  124.         matcher = patternCaseInsPart.matcher(inputStr2);   
  125.         found = matcher.find();   
  126.         System.out.println("Test 8: /" + inputStr2 + "/" + (found  ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");   
  127.   
  128.         System.out.println();   
  129.   
  130.   
  131.         // ---------------------------------------------------------------   
  132.         // Test 3 : (Case Sensitive / Using convenience method matches() )   
  133.         // ---------------------------------------------------------------   
  134.            
  135.         System.out.println("Case Sensitive :: Using convenience method Pattern.matches(Pattern, CharSequence)");   
  136.         System.out.println("---------------------------------------------------------------------------------n");   
  137.   
  138.         found = Pattern.matches(patternStrFull, inputStr1);   
  139.         System.out.println("Test 1: /" + inputStr1 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");   
  140.   
  141.         found = Pattern.matches(patternStrPart, inputStr1);   
  142.         System.out.println("Test 2: /" + inputStr1 + "/" + (found  ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");    
  143.   
  144.         System.out.println();   
  145.   
  146.     }   
  147.   
  148.   
  149.     /**  
  150.      * Sole entry point to the class and application.  
  151.      * @param args Array of String arguments.  
  152.      */  
  153.     public static void main(String[] args) {   
  154.         doRegDemo();   
  155.     }   
  156.   
  157. }   


실행결과


Case sensitive :: matches() v.s. find()
---------------------------------------

Test 1: /AU-120 - Network Cable./ [ MATCHES ]  /^A[^b]-d+ - .+$/

Test 2: /AU-120 - Network Cable./ [ DOES NOT MATCH ]  /^A[^b]-d+ - /

Test 3: /AU-120 - Network Cable./ [ FOUND ]  /^A[^b]-d+ - .+$/

Test 4: /AU-120 - Network Cable./ [ FOUND ]  /^A[^b]-d+ - /

Test 5: /au-120 - Network Cable./ [ DOES NOT MATCH ]  /^A[^b]-d+ - .+$/

Test 6: /au-120 - Network Cable./ [ DOES NOT MATCH ]  /^A[^b]-d+ - /

Test 7: /au-120 - Network Cable./ [ NOT FOUND ]  /^A[^b]-d+ - .+$/

Test 8: /au-120 - Network Cable./ [ NOT FOUND ]  /^A[^b]-d+ - /


Case Insensitive :: matches() v.s. find()
-----------------------------------------

Test 1: /AU-120 - Network Cable./ [ MATCHES ]  /^A[^b]-d+ - .+$/

Test 2: /AU-120 - Network Cable./ [ DOES NOT MATCH ]  /^A[^b]-d+ - /

Test 3: /AU-120 - Network Cable./ [ FOUND ]  /^A[^b]-d+ - .+$/

Test 4: /AU-120 - Network Cable./ [ FOUND ]  /^A[^b]-d+ - /

Test 5: /au-120 - Network Cable./ [ MATCHES ]  /^A[^b]-d+ - .+$/

Test 6: /au-120 - Network Cable./ [ DOES NOT MATCH ]  /^A[^b]-d+ - /

Test 7: /au-120 - Network Cable./ [ FOUND ]  /^A[^b]-d+ - .+$/

Test 8: /au-120 - Network Cable./ [ FOUND ]  /^A[^b]-d+ - /


Case Sensitive :: Using convenience method Pattern.matches(Pattern, CharSequence)
---------------------------------------------------------------------------------

Test 1: /AU-120 - Network Cable./ [ MATCHES ]  /^A[^b]-d+ - .+$/

Test 2: /AU-120 - Network Cable./ [ DOES NOT MATCH ]  /^A[^b]-d+ - /


번호 제목 글쓴이 날짜 조회 수
171 메모리 유출과 약한 참조 황제낙엽 2010.01.26 616
170 Methods of the Matcher Class 황제낙엽 2010.01.19 120
» Pattern.matches() , Matcher.matches() , Matcher.find() file 황제낙엽 2010.01.19 105
168 java.lang.IllegalArgumentException 황제낙엽 2010.01.18 130511
167 org.apache.commons.fileupload.servlet.ServletFileUpload 를 이용한 파일 업로드 file 황제낙엽 2009.11.19 129
166 Error reading tld listeners java.lang.NullPointerException 황제낙엽 2009.10.14 67
165 Cannot find the tag library descriptor for “http://java.sun.com/jsp/jstl/core 황제낙엽 2009.10.14 1006
164 Transfer-Encoding: chunked VS Content-Length 황제낙엽 2009.09.17 154
163 서블릿 응답 헤더(Response Header) 황제낙엽 2009.09.17 80
162 같은 문자열인데도 정규식에서 해당 문자열을 파싱하지 못하는 경우 황제낙엽 2009.08.08 39
161 MultipartRequest (cos.jar)와 서블릿을 이용한 업로드 file 황제낙엽 2009.06.19 384
160 [대용량 파일 업로드] multipart form parser - http file upload, database 저장 java class 연재2 file 황제낙엽 2009.06.19 1831
159 [대용량 파일 업로드] multipart form parser - http file upload 기능 java class 연재1 file 황제낙엽 2009.06.19 1436
158 [reflection/리플렉션] Class.forName 황제낙엽 2009.05.27 101
157 문자열 내의 공백을 제거하는 간단한 정규식 황제낙엽 2009.05.20 88
156 문자열에서 특수 문자 (Escape Sequence) 처리 file 황제낙엽 2009.02.20 1322
155 정규표현식을 사용하는 String클래스의 replaceAll() 함수 개량 황제낙엽 2009.02.09 219
154 File 복사 함수 황제낙엽 2009.02.08 31
153 JSP session 정보 얻기 황제낙엽 2009.01.21 127
152 서버상의 로컬경로 (실제경로) 관련 환경변수 황제낙엽 2009.01.21 339