Tuesday 28 November 2017

What is Regex character classes and how to use simple class and negation | Java Regex


Click here to watch in Youtube :
https://www.youtube.com/watch?v=fQaqYMCKV3k&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
What is Regex character classes and how to use simple class and negation | Java Regex
RegexDemo1.java
import java.util.regex.Pattern;

/**
 * 
 * [abc] = if a or b or c returns true else false.
 *
 */
public class RegexDemo1
{
    public static void main(String[] args)
    {
        /*
         * Parameters:
         * 
         * regex - The expression to be compiled
         * 
         * input - The character sequence to be matched
         * 
         * Returns:
         * 
         * whether or not the regular expression matches on the input
         * 
         */
        
        System.out.println("[abc] matches a = " +   Pattern.matches("[abc]", "a"));
        System.out.println("[abc] matches b = " +   Pattern.matches("[abc]", "b"));
        System.out.println("[abc] matches c = " +   Pattern.matches("[abc]", "c"));
        System.out.println("[abc] matches z = " +   Pattern.matches("[abc]", "z"));
        System.out.println("[abc] matches aa = " +   Pattern.matches("[abc]", "aa"));
        System.out.println("[abc] matches abc = " +   Pattern.matches("[abc]", "abc"));
    }

}
Output
[abc] matches a = true
[abc] matches b = true
[abc] matches c = true
[abc] matches z = false
[abc] matches aa = false
[abc] matches abc = false

RegexDemo2.java
import java.util.regex.Pattern;

/**
 * 
 * [^abc] = if any character except a or b or c returns true else false.
 *
 */
public class RegexDemo2
{
    public static void main(String[] args)
    {
        /*
         * Parameters:
         * 
         * regex - The expression to be compiled
         * 
         * input - The character sequence to be matched
         * 
         * Returns:
         * 
         * whether or not the regular expression matches on the input
         * 
         */
        
        System.out.println("[^abc] matches a = " +   Pattern.matches("[^abc]", "a"));
        System.out.println("[^abc] matches b = " +   Pattern.matches("[^abc]", "b"));
        System.out.println("[^abc] matches c = " +   Pattern.matches("[^abc]", "c"));
        System.out.println("[^abc] matches z = " +   Pattern.matches("[^abc]", "z"));
        System.out.println("[^abc] matches zz = " +   Pattern.matches("[^abc]", "zz"));
        System.out.println("[^abc] matches zzz = " +   Pattern.matches("[^abc]", "zzz"));
    }

}
Output
[^abc] matches a = false
[^abc] matches b = false
[^abc] matches c = false
[^abc] matches z = true
[^abc] matches zz = false
[^abc] matches zzz = false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_char_class_abc.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_char_class_abc

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_char_class_abc/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment