This program tests regular expression matching : Matcher « Regular Expressions « Java






This program tests regular expression matching

  
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
   This program tests regular expression matching.
   Enter a pattern and strings to match, or hit Cancel
   to exit. If the pattern contains groups, the group
   boundaries are displayed in the match.
   @version 1.01 2004-05-11
   @author Cay Horstmann
*/
public class RegExTest
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter pattern: ");
      String patternString = in.nextLine();

      Pattern pattern = null;
      try
      {
         pattern = Pattern.compile(patternString);
      }
      catch (PatternSyntaxException e)
      {
         System.out.println("Pattern syntax error");
         System.exit(1);
      }

      while (true)
      {
         System.out.println("Enter string to match: ");
         String input = in.nextLine();        
         if (input == null || input.equals("")) return;
         Matcher matcher = pattern.matcher(input);
         if (matcher.matches())
         {
            System.out.println("Match");
            int g = matcher.groupCount();
            if (g > 0)
            {
               for (int i = 0; i < input.length(); i++)
               {
                  for (int j = 1; j <= g; j++)
                     if (i == matcher.start(j)) 
                        System.out.print('(');
                  System.out.print(input.charAt(i));
                  for (int j = 1; j <= g; j++)
                     if (i + 1 == matcher.end(j)) 
                        System.out.print(')');
               }
               System.out.println();
            }
         }
         else
            System.out.println("No match");
      }
   }
}

   
    
  








Related examples in the same category

1.Matcher: Find DemoMatcher: Find Demo
2.Matcher ResetMatcher Reset
3.Matcher PatternMatcher Pattern
4.Another Matcher resetAnother Matcher reset
5.Matcher startMatcher start
6.Matcher start with parameterMatcher start with parameter
7.Matcher endMatcher end
8.Matcher end with parameterMatcher end with parameter
9.Matcher groupMatcher group
10.Matcher group with parameterMatcher group with parameter
11.Matcher group countMatcher group count
12.Matcher matchMatcher match
13.Matcher findMatcher find
14.Matcher find with parameterMatcher find with parameter
15.Matcher LookingAtMatcher LookingAt
16.Matcher appendReplacementMatcher appendReplacement
17.Matcher replaceAllMatcher replaceAll
18.Matcher replaceFirstMatcher replaceFirst
19.Matcher group 2Matcher group 2
20.Matcher group with parameter 2Matcher group with parameter 2
21.Matcher ground countMatcher ground count
22.Matcher replaceAll 2Matcher replaceAll 2
23.Matcher find groupMatcher find group
24.Another Matcher find and groupAnother Matcher find and group
25.Pattern compilePattern compile
26.Show line ending matching using Regular Expressions classShow line ending matching using Regular Expressions class
27.Matcher Groups Matcher Groups
28.Matches Looking
29.Match Name Formats
30.Checks whether a string matches a given wildcard pattern
31.Replace all occurances of the target text with the provided replacement text
32.Check if a text is present at the current position in a buffer for string