Java Doc for Matcher.java in  » Scripting » jruby » jregex » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » jruby » jregex 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   jregex.Matcher

Matcher
public class Matcher implements MatchResult(Code)
Matcher instance is an automaton that actually performs matching. It provides the following methods:
  • searching for a matching substrings : matcher.find() or matcher.findAll();
  • testing whether a text matches a whole pattern : matcher.matches();
  • testing whether the text matches the beginning of a pattern : matcher.matchesPrefix();
  • searching with custom options : matcher.find(int options)

    Obtaining results
    After the search succeded, i.e. if one of above methods returned true one may obtain an information on the match:

  • may check whether some group is captured : matcher.isCaptured(int);
  • may obtain start and end positions of the match and its length : matcher.start(int),matcher.end(int),matcher.length(int);
  • may obtain match contents as String : matcher.group(int).
    The same way can be obtained the match prefix and suffix information. The appropriate methods are grouped in MatchResult interface, which the Matcher class implements.
    Matcher objects are not thread-safe, so only one thread may use a matcher instance at a time. Note, that Pattern objects are thread-safe(the same instanse may be shared between multiple threads), and the typical tactics in multithreaded applications is to have one Pattern instance per expression(a singleton), and one Matcher object per thread.


  • Field Summary
    final public static  intACCEPT_INCOMPLETE
         Experimental option; if a text ends up before the end of a pattern,report a match.
    final public static  intANCHOR_END
         The same effect as "$" without REFlags.MULTILINE.
    final public static  intANCHOR_LASTMATCH
         The same effect as "\\G".
    final public static  intANCHOR_START
         The same effect as "^" without REFlags.MULTILINE.

    Constructor Summary
     Matcher(Pattern regex)
        

    Method Summary
    public  charcharAt(int i)
        
    public  charcharAt(int i, int groupId)
        
    final public  intend()
        
    final public  intend(int id)
        
    final public  booleanfind()
         Searches through a target for a matching substring, starting from just after the end of last match.
    final public  booleanfind(int anchors)
         Searches through a target for a matching substring, starting from just after the end of last match.
    public  MatchIteratorfindAll()
        
    public  MatchIteratorfindAll(int options)
         Returns an iterator over the matches found by subsequently calling find(options), the search starts from the zero position.
    public  booleangetGroup(int n, TextBuffer tb)
        
    public  booleangetGroup(String name, TextBuffer tb)
        
    public  booleangetGroup(int n, StringBuffer sb)
        
    public  booleangetGroup(String name, StringBuffer sb)
        
    public  Stringgroup(int n)
        
    public  Stringgroup(String name)
        
    public  intgroupCount()
        
    public  String[]groups()
        
    public  Vectorgroupv()
        
    final public  booleanisCaptured()
        
    final public  booleanisCaptured(int id)
        
    final public  booleanisCaptured(String groupName)
        
    final public  booleanisStart()
         Just an old name for isPrefix().
    Retained for backwards compatibility.
    final public  intlength()
        
    final public  intlength(int id)
        
    final public  booleanmatches()
         Tells whether a current target matches the whole pattern.
    final public  booleanmatches(String s)
         Just a combination of setTarget(String) and matches().
    final public  booleanmatchesPrefix()
         Tells whether the entire target matches the beginning of the pattern.
    public  Patternpattern()
        
    public  Stringprefix()
        
    final public  booleanproceed()
         Continues to search from where the last search left off.
    final public  booleanproceed(int options)
         Continues to search from where the last search left off using specified options:
     Matcher m=new Pattern("\\w+").matcher("abc");
     while(m.proceed(0)){
     System.out.println(m.group(0));
     }
     
    Output:
     abc
     ab
     a
     bc
     b
     c
     
    For example, let's find all odd nubmers occuring in a text:
     Matcher m=new Pattern("\\d+").matcher("123");
     while(m.proceed(0)){
     String match=m.group(0);
     if(isOdd(Integer.parseInt(match))) System.out.println(match);
     }
     static boolean isOdd(int i){
     return (i&1)>0;
     }
     
    This outputs:
     123
     1
     23
     3
     
    Note that using find() method we would find '123' only.
    public  voidsetOffset(int offset)
        
    public  voidsetPosition(int pos)
         Allows to set a position the subsequent find()/find(int) will start from.
    final public  voidsetTarget(Matcher m, int groupId)
         This method allows to efficiently pass data between matchers.
    public  voidsetTarget(String text)
         Supplies a text to search in/match with.
    public  voidsetTarget(String text, int start, int len)
         Supplies a text to search in/match with, as a part of String.
    public  voidsetTarget(char[] text, int start, int len)
         Supplies a text to search in/match with, as a part of char array.
    final public  voidsetTarget(char[] text, int start, int len, boolean shared)
         To be used with much care. Supplies a text to search in/match with, as a part of a char array, as above, but also allows to permit to use the array as internal buffer for subsequent inputs.
    public  voidsetTarget(Reader in, int len)
         Supplies a text to search in/match with through a stream.
    final public  voidskip()
         Sets the current search position just after the end of last match.
    final public  intstart()
        
    final public  intstart(int id)
        
    public  Stringsuffix()
        
    public  Stringtarget()
        
    public  char[]targetChars()
        
    public  inttargetEnd()
        
    public  inttargetStart()
        
    public  StringtoString()
        
    public  StringtoString_d()
        

    Field Detail
    ACCEPT_INCOMPLETE
    final public static int ACCEPT_INCOMPLETE(Code)
    Experimental option; if a text ends up before the end of a pattern,report a match.
    See Also:   Matcher.find(int)



    ANCHOR_END
    final public static int ANCHOR_END(Code)
    The same effect as "$" without REFlags.MULTILINE.
    See Also:   Matcher.find(int)



    ANCHOR_LASTMATCH
    final public static int ANCHOR_LASTMATCH(Code)
    The same effect as "\\G".
    See Also:   Matcher.find(int)



    ANCHOR_START
    final public static int ANCHOR_START(Code)
    The same effect as "^" without REFlags.MULTILINE.
    See Also:   Matcher.find(int)




    Constructor Detail
    Matcher
    Matcher(Pattern regex)(Code)




    Method Detail
    charAt
    public char charAt(int i)(Code)



    charAt
    public char charAt(int i, int groupId)(Code)



    end
    final public int end()(Code)



    end
    final public int end(int id)(Code)



    find
    final public boolean find()(Code)
    Searches through a target for a matching substring, starting from just after the end of last match. If there wasn't any search performed, starts from zero. true if a match found.



    find
    final public boolean find(int anchors)(Code)
    Searches through a target for a matching substring, starting from just after the end of last match. If there wasn't any search performed, starts from zero.
    Parameters:
      anchors - a zero or a combination(bitwise OR) of ANCHOR_START,ANCHOR_END,ANCHOR_LASTMATCH,ACCEPT_INCOMPLETE true if a match found.



    findAll
    public MatchIterator findAll()(Code)
    The same as findAll(int), but with default behaviour;



    findAll
    public MatchIterator findAll(int options)(Code)
    Returns an iterator over the matches found by subsequently calling find(options), the search starts from the zero position.



    getGroup
    public boolean getGroup(int n, TextBuffer tb)(Code)



    getGroup
    public boolean getGroup(String name, TextBuffer tb)(Code)



    getGroup
    public boolean getGroup(int n, StringBuffer sb)(Code)



    getGroup
    public boolean getGroup(String name, StringBuffer sb)(Code)



    group
    public String group(int n)(Code)



    group
    public String group(String name)(Code)



    groupCount
    public int groupCount()(Code)



    groups
    public String[] groups()(Code)



    groupv
    public Vector groupv()(Code)



    isCaptured
    final public boolean isCaptured()(Code)



    isCaptured
    final public boolean isCaptured(int id)(Code)



    isCaptured
    final public boolean isCaptured(String groupName)(Code)



    isStart
    final public boolean isStart()(Code)
    Just an old name for isPrefix().
    Retained for backwards compatibility.



    length
    final public int length()(Code)



    length
    final public int length(int id)(Code)



    matches
    final public boolean matches()(Code)
    Tells whether a current target matches the whole pattern. For example the following yields the true:
     Pattern p=new Pattern("\\w+"); 
     p.matcher("a").matches();
     p.matcher("ab").matches();
     p.matcher("abc").matches();
     
    and the following yields the false:
     p.matcher("abc def").matches();
     p.matcher("bcd ").matches();
     p.matcher(" bcd").matches();
     p.matcher("#xyz#").matches();
     
    whether a current target matches the whole pattern.



    matches
    final public boolean matches(String s)(Code)
    Just a combination of setTarget(String) and matches().
    Parameters:
      s - the target string; whether the specified string matches the whole pattern.



    matchesPrefix
    final public boolean matchesPrefix()(Code)
    Tells whether the entire target matches the beginning of the pattern. The whole pattern is also regarded as its beginning.
    This feature allows to find a mismatch by examining only a beginning part of the target (as if the beginning of the target doesn't match the beginning of the pattern, then the entire target also couldn't match).
    For example the following assertions yield true:
     Pattern p=new Pattern("abcd"); 
     p.matcher("").matchesPrefix();
     p.matcher("a").matchesPrefix();
     p.matcher("ab").matchesPrefix();
     p.matcher("abc").matchesPrefix();
     p.matcher("abcd").matchesPrefix();
     
    and the following yield false:
     p.matcher("b").isPrefix();
     p.matcher("abcdef").isPrefix();
     p.matcher("x").isPrefix();
     
    true if the entire target matches the beginning of the pattern



    pattern
    public Pattern pattern()(Code)



    prefix
    public String prefix()(Code)



    proceed
    final public boolean proceed()(Code)
    Continues to search from where the last search left off. The same as proceed(0).
    See Also:   Matcher.proceed(int)



    proceed
    final public boolean proceed(int options)(Code)
    Continues to search from where the last search left off using specified options:
     Matcher m=new Pattern("\\w+").matcher("abc");
     while(m.proceed(0)){
     System.out.println(m.group(0));
     }
     
    Output:
     abc
     ab
     a
     bc
     b
     c
     
    For example, let's find all odd nubmers occuring in a text:
     Matcher m=new Pattern("\\d+").matcher("123");
     while(m.proceed(0)){
     String match=m.group(0);
     if(isOdd(Integer.parseInt(match))) System.out.println(match);
     }
     static boolean isOdd(int i){
     return (i&1)>0;
     }
     
    This outputs:
     123
     1
     23
     3
     
    Note that using find() method we would find '123' only.
    Parameters:
      options - search options, some of ANCHOR_START|ANCHOR_END|ANCHOR_LASTMATCH|ACCEPT_INCOMPLETE; zero value(default) stands for usual search for substring.



    setOffset
    public void setOffset(int offset)(Code)



    setPosition
    public void setPosition(int pos)(Code)
    Allows to set a position the subsequent find()/find(int) will start from.
    Parameters:
      pos - the position to start from;
    See Also:   Matcher.find
    See Also:   Matcher.find(int)



    setTarget
    final public void setTarget(Matcher m, int groupId)(Code)
    This method allows to efficiently pass data between matchers. Note that a matcher may pass data to itself:
     Matcher m=new Pattern("\\w+").matcher(myString);
     if(m.find())m.setTarget(m,m.SUFFIX); //forget all that is not a suffix
     
    Resets current search position to zero.
    Parameters:
      m - - a matcher that is a source of data
    Parameters:
      groupId - - which group to take data from
    See Also:   Matcher.setTarget(java.lang.String)
    See Also:   Matcher.setTarget(java.lang.Stringintint)
    See Also:   Matcher.setTarget(char[]intint)
    See Also:   Matcher.setTarget(java.io.Readerint)



    setTarget
    public void setTarget(String text)(Code)
    Supplies a text to search in/match with. Resets current search position to zero.
    Parameters:
      text - - a data
    See Also:   Matcher.setTarget(jregex.Matcherint)
    See Also:   Matcher.setTarget(java.lang.Stringintint)
    See Also:   Matcher.setTarget(char[]intint)
    See Also:   Matcher.setTarget(java.io.Readerint)



    setTarget
    public void setTarget(String text, int start, int len)(Code)
    Supplies a text to search in/match with, as a part of String. Resets current search position to zero.
    Parameters:
      text - - a data source
    Parameters:
      start - - where the target starts
    Parameters:
      len - - how long is the target
    See Also:   Matcher.setTarget(jregex.Matcherint)
    See Also:   Matcher.setTarget(java.lang.String)
    See Also:   Matcher.setTarget(char[]intint)
    See Also:   Matcher.setTarget(java.io.Readerint)



    setTarget
    public void setTarget(char[] text, int start, int len)(Code)
    Supplies a text to search in/match with, as a part of char array. Resets current search position to zero.
    Parameters:
      text - - a data source
    Parameters:
      start - - where the target starts
    Parameters:
      len - - how long is the target
    See Also:   Matcher.setTarget(jregex.Matcherint)
    See Also:   Matcher.setTarget(java.lang.String)
    See Also:   Matcher.setTarget(java.lang.Stringintint)
    See Also:   Matcher.setTarget(java.io.Readerint)



    setTarget
    final public void setTarget(char[] text, int start, int len, boolean shared)(Code)
    To be used with much care. Supplies a text to search in/match with, as a part of a char array, as above, but also allows to permit to use the array as internal buffer for subsequent inputs. That is, if we call it with shared=false:
     myMatcher.setTarget(myCharArray,x,y,false); //we declare that array contents is NEITHER shared NOR will be used later, so may modifications on it are permitted
     
    then we should expect the array contents to be changed on subsequent setTarget(..) operations. Such method may yield some increase in perfomanse in the case of multiple setTarget() calls. Resets current search position to zero.
    Parameters:
      text - - a data source
    Parameters:
      start - - where the target starts
    Parameters:
      len - - how long is the target
    Parameters:
      shared - - if true: data are shared or used later, don't modify it; if false: possible modifications of the text on subsequent setTarget() calls are perceived and allowed.
    See Also:   Matcher.setTarget(jregex.Matcherint)
    See Also:   Matcher.setTarget(java.lang.String)
    See Also:   Matcher.setTarget(java.lang.Stringintint)
    See Also:   Matcher.setTarget(char[]intint)
    See Also:   Matcher.setTarget(java.io.Readerint)



    setTarget
    public void setTarget(Reader in, int len) throws IOException(Code)
    Supplies a text to search in/match with through a stream. Resets current search position to zero.
    Parameters:
      in - - a data stream;
    Parameters:
      len - - how much characters should be read; if len is -1, read the entire stream.
    See Also:   Matcher.setTarget(jregex.Matcherint)
    See Also:   Matcher.setTarget(java.lang.String)
    See Also:   Matcher.setTarget(java.lang.Stringintint)
    See Also:   Matcher.setTarget(char[]intint)



    skip
    final public void skip()(Code)
    Sets the current search position just after the end of last match.



    start
    final public int start()(Code)



    start
    final public int start(int id)(Code)



    suffix
    public String suffix()(Code)



    target
    public String target()(Code)



    targetChars
    public char[] targetChars()(Code)



    targetEnd
    public int targetEnd()(Code)



    targetStart
    public int targetStart()(Code)



    toString
    public String toString()(Code)



    toString_d
    public String toString_d()(Code)



    Methods inherited from java.lang.Object
    native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
    public boolean equals(Object obj)(Code)(Java Doc)
    protected void finalize() throws Throwable(Code)(Java Doc)
    final native public Class getClass()(Code)(Java Doc)
    native public int hashCode()(Code)(Java Doc)
    final native public void notify()(Code)(Java Doc)
    final native public void notifyAll()(Code)(Java Doc)
    public String toString()(Code)(Java Doc)
    final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
    final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
    final public void wait() throws InterruptedException(Code)(Java Doc)

    www.java2java.com | Contact Us
    Copyright 2009 - 12 Demo Source and Support. All rights reserved.
    All other trademarks are property of their respective owners.