title: Problem using Matcher and Pattern objects in Java
body: <p>I am trying to make a Lexer.  I am using a Matcher object to get the next token from an HTML String.  I am trying to use the lookingAt() method of the Matcher to get the first occurance of the POSIX expression I am looking for.  The problem is group() is supposed to print out only that phrase that matches the expression but instead it prints out the whole HTML String.  Here is the code:</p>
  
  <pre><code>public static final String[] DEFAULT_RULES = new String[] {         
              // PUT YOUR REGULAR EXPRESSIONS HERE.  SEE THE ORDER BELOW
              "&lt;!--.*--&gt;",                                    // A comment TESTED
              "&lt;\\p{Alnum}+.*\\p{Blank}*/&gt;",                  // Singular Tag
              "&lt;\\p{Alnum}+.*[^/]*&gt;",                         // Opening Tag TESTED
              "&lt;/\\p{Alnum}+\\p{Space}*&gt;",                    // Closing Tag TESTED
              "&amp;.*;",                                         // HTTP Entity TESTED
              ".*"    };
  </code></pre>
  
  <p>METHOD: </p>
  
  <pre><code>    for( int i = 0; i &lt; DEFAULT_RULES.length; i++ ) {// Loop through each expression and try to find a matching phrase
          pattern = Pattern.compile( DEFAULT_RULES[i], Pattern.DOTALL );  // Get a Regex Pattern
          matcher = pattern.matcher( mainString );    // Check if Pattern matches the String
  
          //matcher.region( position, mainString.length() );  // Make the Region start from the current pointer to the end
  
          if( matcher.lookingAt() ) {     // Match found at current position
              int s = matcher.start();
              int e = matcher.end();
              String nextToken = matcher.group();     // Save the current phrase that matched the expression
              position = matcher.end();           // Move position pointer to the character after the end of the Token
              return nextToken;// return the Token
          }
      }
  </code></pre>
  
  <p><strong>NOTE:</strong> DEFAULT_RULES is a list of expression strings that I am looking for.  The ouput I am expecting is:</p>
  
  <pre><code>&lt;P&gt;
  </code></pre>
  
  <p>but instead I get the whole HTML file.  I hope this makes sense.</p>