tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Basic > Char Matcher Negate

Char Matcher Negate 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using CharMatcher.negate() API. It returns a char matcher that matches any character not matched by the current matcher.

File Name  :  
com/bethecoder/tutorials/guava/base_tests/CharMatcherNegateTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.base_tests;

import com.google.common.base.CharMatcher;

public class CharMatcherNegateTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    System.out.println(CharMatcher.is('a').negate().matches('a'));
    System.out.println(CharMatcher.is('a').negate().matches('1'));
    System.out.println(CharMatcher.is('a').negate().matches('@'));
    
    System.out.println(CharMatcher.anyOf("abc123").negate().matches('1'));
    System.out.println(CharMatcher.anyOf("abc123").negate().matches('a'));
    System.out.println(CharMatcher.anyOf("abc123").negate().matches('z'));
    System.out.println(CharMatcher.anyOf("abc123").negate().matches('9'));
  }
}
   

It gives the following output,
false
true
true

false
false
true
true



 
  


  
bl  br