Tuesday 28 November 2017

How to use intersection and subtraction Regex character classes | Java Regex


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

Click the below Image to Enlarge
How to use intersection and subtraction Regex character classes | Java Regex
RegexDemo1.java
import java.util.regex.Pattern;

/**
 * 
 * [a-z&&[def]] = d, e, or f (intersection)
 *
 */
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("[a-z&&[def]] matches d = " +   Pattern.matches("[a-z&&[def]]", "d"));
        System.out.println("[a-z&&[def]] matches e = " +   Pattern.matches("[a-z&&[def]]", "e"));
        System.out.println("[a-z&&[def]] matches f = " +   Pattern.matches("[a-z&&[def]]", "f"));
        System.out.println("[a-z&&[def]] matches z = " +   Pattern.matches("[a-z&&[def]]", "z"));
        System.out.println("[a-z&&[def]] matches def = " +   Pattern.matches("[a-z&&[def]]", "def"));
    }

}
Output
[a-z&&[def]] matches d = true
[a-z&&[def]] matches e = true
[a-z&&[def]] matches f = true
[a-z&&[def]] matches z = false
[a-z&&[def]] matches def = false

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

/**
 * 
 * [a-z&&[^bc]] = a through z, except for b and c
 *
 */
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("[a-z&&[^bc]] matches h = " +   Pattern.matches("[a-z&&[^bc]]", "h"));
        System.out.println("[a-z&&[^bc]] matches c = " +   Pattern.matches("[a-z&&[^bc]]", "c"));
        System.out.println("[a-z&&[^bc]] matches hh = " +   Pattern.matches("[a-z&&[^bc]]", "hh"));
    }

}
Output
[a-z&&[^bc]] matches h = true
[a-z&&[^bc]] matches c = false
[a-z&&[^bc]] matches hh = false

RegexDemo3.java
import java.util.regex.Pattern;

/**
 * 
 * [a-z&&[^m-p]] = a through z, and not m through p
 *
 */
public class RegexDemo3
{
    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("[a-z&&[^m-p]] matches b = " +   Pattern.matches("[a-z&&[^m-p]]", "b"));
        System.out.println("[a-z&&[^m-p]] matches o = " +   Pattern.matches("[a-z&&[^m-p]]", "o"));
        System.out.println("[a-z&&[^m-p]] matches bb = " +   Pattern.matches("[a-z&&[^m-p]]", "bb"));
    }

}
Output
[a-z&&[^m-p]] matches b = true
[a-z&&[^m-p]] matches o = false
[a-z&&[^m-p]] matches bb = false

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_char_class_intersection/?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
  • 2 comments: