Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

RegexCollection



The RegexCollection class allows to specify a list of regex patterns and match a CharSequence against all the patterns. It simplifies the usage of several regex expressions against the same input.

Configuration

The RegexCollection.acceptUnmatchedPattern(boolean) allow to set if the collection accept the unmatched pattern. If true, it means that if no pattern in the collection has a matcher which matches the input, then the RegexMatcher.UNMATCHED instance will be returned.

By default the collection does accept the unmatched pattern, which means that if no pattern in the collection has a matcher which matches the input, the last ordered pattern in the list will be returned.

Algorithm

Default behavior

The class iterate from the first specified pattern to the last one:
  • If a pattern matches, the iteration stops and the associated RegexMatcher will be returned
  • If the last pattern does not match, the iteration stops on the associated RegexMatcher

Behavior if the collection accept the unmatched patter

The class iterate from the first specified pattern to the last one:
The name of the RegexPattern.UNMATCHED pattern is "--UNMATCHED--".

RegexMatcher result

The RegexMatcher class encapsulates a java.util.regex.Matcher. The main methods of this class are: This class encapsulates a lot of methods of the Matcher class. For example:
   RegexMatcher matcher = collection.matcher("156");
   String group = matcher.group(2);
is equivalent to:
   RegexMatcher matcher = collection.matcher("156");
   String group = matcher.getMatcher().group(2);

Examples

Basic usage

This example shows a simple usage of the class:
   RegexCollection collection = new RegexCollection();
   collection.addPattern("number", "\\d+");
   collection.addPattern("letters", "[A-Za-z]+");
      
   RegexMatcher matcher = collection.matcher("156");
   String name = matcher.getPatternName(); // return "number"
   boolean matches = matcher.matches(); // return true

   matcher = collection.matcher("Slayer");
   name = matcher.getPatternName(); // return "letters"
   matches = matcher.matches(); // return true

   matcher = collection.matcher("1349 band");
   name = matcher.getPatternName(); // return "letters"
   matches = matcher.matches(); // return false      

Usage with a switch case

This example shows how you can use the class with a switch case:
   RegexCollection collection = new RegexCollection();
   collection.addPattern("number", "\\d+");
   collection.addPattern("letters", "[A-Za-z]+");
   collection.addPattern("bother", "[A-Za-z]+ \\d+");
      
   RegexMatcher matcher = collection.matcher("156");
   if (matcher.matches()) {
      switch (matcher.getPatternName()) {
        case "number":
             System.out.println("This is a number";
             break;
        case "letters":
             System.out.println("This is a word";
             break;      
        case "both":
             System.out.println("This is a word followed by a number";
             break;        
      }
   }     

Result wheen no pattern matches

   RegexCollection collection = new RegexCollection();
   collection.addPattern("number", "\\d+");
   collection.addPattern("letters", "[A-Za-z]+");
   collection.acceptUnmatchedPattern(true);

   RegexMatcher matcher = collection.matcher("1349 band"); // does not match any of the patterns
   String name = matcher.getPatternName(); // return "--UNMATCHED--" (the unmatched pattern)
   boolean isUnmatched = matcher.isUnmatched(); // return true
   boolean matches = matcher.matches(); // return false

Result wheen no pattern matches and the collection accept the unmatched patter

   RegexCollection collection = new RegexCollection();
   collection.addPattern("number", "\\d+");
   collection.addPattern("letters", "[A-Za-z]+");

   RegexMatcher matcher = collection.matcher("1349 band"); // does not match any of the patterns
   String name = matcher.getPatternName(); // return "letters" (the last pattern)
   boolean matches = matcher.matches(); // return false

See also


Categories: Packages | Util

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences