SSISO Community

시소당

Matcher의 matches() 와 find()의 차이 보는 소스

//  -----------------------------------------------------------------------------
//  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();
        }

}

출처  :  http://blog.naver.com/94eun?Redirect=Log&logNo=100003096754

1305 view

4.0 stars