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" 형태가 맞느냐? 라고 찾기 때문이다.
이를 비교한 샘플 프로그램이다.
- // -----------------------------------------------------------------------------
- // RegDemoSun.java
- // -----------------------------------------------------------------------------
- import java.util.regex.*;
- /**
- * -----------------------------------------------------------------------------
- * Used to provide an example of how to utilize the Java 2 Regular Expression
- * Implementation from Sun. It is important to note that a Regular Expression
- * implentation didn't appear in the Java SDK until version 1.4.
- *
- * Sun's implementation of its regular expression parser enables you to both
- * "find" and "match" character sequences against a particular pattern. Using
- * "find" enables the user to find matches in a string while "match" requires
- * an EXACT match of a regular expression.
- * -----------------------------------------------------------------------------
- */
- public class RegDemoSun {
- private static void doRegDemo() {
- String patternStrFull = "^A[^b]-d+ - .+$";
- String patternStrPart = "^A[^b]-d+ - ";
- CharSequence inputStr1 = "AU-120 - Network Cable.";
- CharSequence inputStr2 = "au-120 - Network Cable.";
- Pattern patternFull = Pattern.compile(patternStrFull);
- Pattern patternPart = Pattern.compile(patternStrPart);
- Pattern patternCaseInsFull = Pattern.compile(patternStrFull, Pattern.CASE_INSENSITIVE);
- Pattern patternCaseInsPart = Pattern.compile(patternStrPart, Pattern.CASE_INSENSITIVE);
- Matcher matcher = null;
- String matchStr = " [ MATCHES ] ";
- String noMatchStr = " [ DOES NOT MATCH ] ";
- String foundStr = " [ FOUND ] ";
- String noFoundStr = " [ NOT FOUND ] ";
- boolean found = false;
- System.out.println();
- // -------------------------------------------------------
- // Test 1 : (Case Sensitive / match() v.s. find() )
- // -------------------------------------------------------
- System.out.println("Case sensitive :: matches() v.s. find()");
- System.out.println("---------------------------------------n");
- matcher = patternFull.matcher(inputStr1);
- found = matcher.matches();
- System.out.println("Test 1: /" + inputStr1 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");
- matcher = patternPart.matcher(inputStr1);
- found = matcher.matches();
- System.out.println("Test 2: /" + inputStr1 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");
- matcher = patternFull.matcher(inputStr1);
- found = matcher.find();
- System.out.println("Test 3: /" + inputStr1 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");
- matcher = patternPart.matcher(inputStr1);
- found = matcher.find();
- System.out.println("Test 4: /" + inputStr1 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");
- matcher = patternFull.matcher(inputStr2);
- found = matcher.matches();
- System.out.println("Test 5: /" + inputStr2 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");
- matcher = patternPart.matcher(inputStr2);
- found = matcher.matches();
- System.out.println("Test 6: /" + inputStr2 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");
- matcher = patternFull.matcher(inputStr2);
- found = matcher.find();
- System.out.println("Test 7: /" + inputStr2 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");
- matcher = patternPart.matcher(inputStr2);
- found = matcher.find();
- System.out.println("Test 8: /" + inputStr2 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");
- System.out.println();
- // -------------------------------------------------------
- // Test 2 : (Case Insensitive / match() v.s. find() )
- // -------------------------------------------------------
- System.out.println("Case Insensitive :: matches() v.s. find()");
- System.out.println("-----------------------------------------n");
- matcher = patternCaseInsFull.matcher(inputStr1);
- found = matcher.matches();
- System.out.println("Test 1: /" + inputStr1 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");
- matcher = patternCaseInsPart.matcher(inputStr1);
- found = matcher.matches();
- System.out.println("Test 2: /" + inputStr1 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");
- matcher = patternCaseInsFull.matcher(inputStr1);
- found = matcher.find();
- System.out.println("Test 3: /" + inputStr1 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");
- matcher = patternCaseInsPart.matcher(inputStr1);
- found = matcher.find();
- System.out.println("Test 4: /" + inputStr1 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");
- matcher = patternCaseInsFull.matcher(inputStr2);
- found = matcher.matches();
- System.out.println("Test 5: /" + inputStr2 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");
- matcher = patternCaseInsPart.matcher(inputStr2);
- found = matcher.matches();
- System.out.println("Test 6: /" + inputStr2 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");
- matcher = patternCaseInsFull.matcher(inputStr2);
- found = matcher.find();
- System.out.println("Test 7: /" + inputStr2 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrFull + "/n");
- matcher = patternCaseInsPart.matcher(inputStr2);
- found = matcher.find();
- System.out.println("Test 8: /" + inputStr2 + "/" + (found ?foundStr : noFoundStr) + "/" + patternStrPart + "/n");
- System.out.println();
- // ---------------------------------------------------------------
- // Test 3 : (Case Sensitive / Using convenience method matches() )
- // ---------------------------------------------------------------
- System.out.println("Case Sensitive :: Using convenience method Pattern.matches(Pattern, CharSequence)");
- System.out.println("---------------------------------------------------------------------------------n");
- found = Pattern.matches(patternStrFull, inputStr1);
- System.out.println("Test 1: /" + inputStr1 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrFull + "/n");
- found = Pattern.matches(patternStrPart, inputStr1);
- System.out.println("Test 2: /" + inputStr1 + "/" + (found ?matchStr : noMatchStr) + "/" + patternStrPart + "/n");
- System.out.println();
- }
- /**
- * Sole entry point to the class and application.
- * @param args Array of String arguments.
- */
- public static void main(String[] args) {
- doRegDemo();
- }
- }
실행결과
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+ - /
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
13 | Methods of the Matcher Class | 황제낙엽 | 2010.01.19 | 431 |
» |
Pattern.matches() , Matcher.matches() , Matcher.find()
![]() | 황제낙엽 | 2010.01.19 | 595 |
11 | 같은 문자열인데도 정규식에서 해당 문자열을 파싱하지 못하는 경우 | 황제낙엽 | 2009.08.08 | 367 |
10 | 문자열 내의 공백을 제거하는 간단한 정규식 | 황제낙엽 | 2009.05.20 | 399 |
9 | 정규표현식을 사용하는 String클래스의 replaceAll() 함수 개량 | 황제낙엽 | 2009.02.09 | 507 |
8 | 입력받은 문자열의 의미가 숫자인지 단순 텍스트인지 판별해야 할 때 | 황제낙엽 | 2009.01.09 | 373 |
7 | 사용팁 | 황제낙엽 | 2008.07.24 | 296 |
6 | 정규식 사용예제 [2] | 황제낙엽 | 2008.06.11 | 384 |
5 | 정규식 사용예제 [1] | 황제낙엽 | 2008.06.11 | 436 |
4 | java String.replaceAll (String regex, String replacement) 쓸떄 조심할 것 | 황제낙엽 | 2008.05.22 | 424 |
3 | java String.replaceAll 잘쓰기 | 황제낙엽 | 2008.05.22 | 429 |
2 | Jakarta 프로젝트의 Regexp(정규식) 패키지 사용하기 | 황제낙엽 | 2007.01.22 | 285 |
1 |
PATTERN MATCHING (패턴 매칭)
![]() | 황제낙엽 | 2007.01.17 | 520 |