Thursday, 30 November 2017

How to use Regex finder to find a word | Java Regex | Java Regular Expressions | Regex in java


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

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

/**
 * 
 * Java Regex Finder Example
 *
 */

public class RegexDemo
{
    public static void main(String[] args)
    {
        try (Scanner sc = new Scanner(System.in))
        {
            while (true)
            {
                System.out.println("Enter regex pattern:");
                String regexPattern = sc.nextLine();                
                Pattern pattern = Pattern.compile(regexPattern);
                Matcher matcher = pattern.matcher("Welcome to india peter");
                boolean found = false;
                while (matcher.find())
                {
                    System.out.println("I found the text "
                            + matcher.group() + " starting at index "
                            + matcher.start()
                            + " and ending at index "
                            + matcher.end());
                    found = true;
                }
                if (!found)
                {
                    System.out.println("No match found.");
                }
            }
        }

    }

}
Output
Enter regex pattern:
india
I found the text india starting at index 11 and ending at index 16
Enter regex pattern:
peter
I found the text peter starting at index 17 and ending at index 22

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_finder/?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
  • How to create a regex that accepts alphanumeric characters and length is 4 | Java Regex


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

    RegexDemo.java
    import java.util.regex.Pattern;
    
    /**
     *
     * Create a regular expression that accepts alpha numeric characters only. Its
     * length must be 4 characters long only.
     *
     */
    
    public class RegexDemo
    {
        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(Pattern.matches("[a-zA-Z0-9]{4}", "Juli"));//true
            System.out.println(Pattern.matches("[a-zA-Z0-9]{4}", "Ju22"));//true
            System.out.println(Pattern.matches("[a-zA-Z0-9]{4}", "Peter"));//false (more than 4 char)
            System.out.println(Pattern.matches("[a-zA-Z0-9]{4}", "J$22"));//false ($ is not matched)
        }
    
    }
    
    Output
    true
    true
    false
    false
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_alphanumeric_length_4.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_alphanumeric_length_4/?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
  • How to use word and non-word Regex meta-characters | Java Regex


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

    Click the below Image to Enlarge
    How to use word and non-word Regex meta-characters | Java Regex
    RegexDemo1.java
    import java.util.regex.Pattern;
    
    /**
     * 
     * \w = Any word character, short for [a-zA-Z_0-9]
     *
     */
    
    public class RegexDemo1
    {
        public static void main(String[] args)
        {
            System.out.println("metacharacters w");
            /*
             * 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(Pattern.matches("\\w", "a"));//true
            System.out.println(Pattern.matches("\\w", "9"));//true
            System.out.println(Pattern.matches("\\w", "_"));//true
            System.out.println(Pattern.matches("\\w", "a9"));//false
            
            System.out.println("----------------------------------");
            
            System.out.println("metacharacters w with quantifier....");  
            System.out.println(Pattern.matches("\\w*", "peter_99"));//true
            System.out.println(Pattern.matches("\\w*", "peter_99@"));//false
        }
    
    }
    
    Output
    metacharacters w
    true
    true
    true
    false
    ----------------------------------
    metacharacters w with quantifier....
    true
    false
    
    
    RegexDemo2.java
    import java.util.regex.Pattern;
    
    /**
     * 
     * \W = Any non-word character, short for [^\w]
     *
     */
    
    public class RegexDemo2
    {
        public static void main(String[] args)
        {
            System.out.println("metacharacters W");
            /*
             * 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(Pattern.matches("\\W", "@"));//true
            System.out.println(Pattern.matches("\\W", "a"));//false
            System.out.println(Pattern.matches("\\W", "9"));//false
            System.out.println(Pattern.matches("\\W", "@@"));//false
            
            System.out.println("----------------------------------");
            
            System.out.println("metacharacters W with quantifier....");  
            System.out.println(Pattern.matches("\\W*", "@@"));//true
            System.out.println(Pattern.matches("\\W*", "@@peter"));//false
        }
    
    }
    
    Output
    metacharacters W
    true
    false
    false
    false
    ----------------------------------
    metacharacters W with quantifier....
    true
    false
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_metachars_slash_w.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_metachars_slash_w/?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
  • How to use whitespace and non-whitespace Regex meta-characters | Java Regex


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

    Click the below Image to Enlarge
    How to use whitespace and non-whitespace Regex meta-characters | Java Regex
    RegexDemo1.java
    import java.util.regex.Pattern;
    
    /**
     * 
     * \s = Any whitespace character should come only one time, short for [\t\n\x0B\f\r]
     *
     */
    
    public class RegexDemo1
    {
        public static void main(String[] args)
        {
            System.out.println("metacharacters s");
            /*
             * 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(Pattern.matches("\\s", " "));//true [Only one whitespace can come]
            System.out.println(Pattern.matches("\\s", "\t"));//true [Only one whitespace can come]
            System.out.println(Pattern.matches("\\s", "\n"));//true [Only one whitespace can come]
            System.out.println(Pattern.matches("\\s", "\f"));//true [Only one whitespace can come]
            System.out.println(Pattern.matches("\\s", "     "));//false [Only one whitespace can come, but more whitespaces]
            System.out.println(Pattern.matches("\\s", "\t\t"));//false [Only one whitespace can come, but more whitespaces]
            System.out.println(Pattern.matches("\\s", "ab"));//false [not a whitespace]
            
            System.out.println("----------------------------------");
            
            System.out.println("metacharacters s with quantifier....");  
            System.out.println(Pattern.matches("\\s*", "       "));//true [more than one whitespace can come]
            System.out.println(Pattern.matches("\\s*", "\r\r"));//true [more than one whitespace can come]
        }
    
    }
    
    Output
    metacharacters s
    true
    true
    true
    true
    false
    false
    false
    ----------------------------------
    metacharacters s with quantifier....
    true
    true
    
    
    RegexDemo2.java
    import java.util.regex.Pattern;
    
    /**
     * 
     * \S = Any non-whitespace character Should come only one time, short for [^\s]
     *
     */
    
    public class RegexDemo2
    {
        public static void main(String[] args)
        {
            System.out.println("metacharacters S");
            /*
             * 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(Pattern.matches("\\S", "a"));//true 
            System.out.println(Pattern.matches("\\S", "ab"));//false 
            System.out.println(Pattern.matches("\\S", " "));//false 
            System.out.println(Pattern.matches("\\S", "\t"));//false
            System.out.println(Pattern.matches("\\S", "\n"));//false 
            System.out.println(Pattern.matches("\\S", "\f"));//false 
        
            
            System.out.println("----------------------------------");
            
            System.out.println("metacharacters S with quantifier....");  
            System.out.println(Pattern.matches("\\S*", "peter"));//true
        }
    
    }
    
    Output
    metacharacters S
    true
    false
    false
    false
    false
    false
    ----------------------------------
    metacharacters S with quantifier....
    true
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_metachars_slash_s.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_metachars_slash_s/?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
  • Tuesday, 28 November 2017

    How to use any character, digit and non-digit Regex meta-characters | Java Regex


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

    Click the below Image to Enlarge
    How to use any character, digit and non-digit Regex meta-characters | Java Regex
    RegexDemo1.java
    import java.util.regex.Pattern;
    
    /**
     * 
     * \d = Any digits, short of [0-9]
     *
     */
    
    public class RegexDemo1
    {
        public static void main(String[] args)
        {
            System.out.println("metacharacters d");
            
            /*
             * 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(Pattern.matches("\\d", "9"));//true (digit and comes once)
            System.out.println(Pattern.matches("\\d", "dfg"));//false (non-digit)  
            System.out.println(Pattern.matches("\\d", "8888"));//false (digit but comes more than once)  
            System.out.println(Pattern.matches("\\d", "555abc"));//false (digit and char)  
            
            System.out.println("----------------------------------");
            
            System.out.println("metacharacters d with quantifier....");  
            System.out.println(Pattern.matches("\\d*", "56565"));//true (digit may come 0 or more times)
        }
    
    }
    
    Output
    metacharacters d
    true
    false
    false
    false
    ----------------------------------
    metacharacters d with quantifier....
    true
    
    
    RegexDemo2.java
    import java.util.regex.Pattern;
    
    /**
     * 
     * \D = Any non-digit, short for [^0-9]
     *
     */
    public class RegexDemo2
    {
        public static void main(String[] args)
        {
            System.out.println("metacharacters D..");  
            /*
             * 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(Pattern.matches("\\D", "m"));//true (non-digit and comes once)
            System.out.println(Pattern.matches("\\D", "xds"));//false (non-digit but comes more than once)  
            System.out.println(Pattern.matches("\\D", "1"));//false (digit)  
            System.out.println(Pattern.matches("\\D", "8989"));//false (digit)  
            System.out.println(Pattern.matches("\\D", "121abc"));//false (digit and char)  
            
            System.out.println("----------------------------------");
            
            System.out.println("metacharacters D with quantifier....");  
            System.out.println(Pattern.matches("\\D*", "peter"));//true (non-digit and may come 0 or more times)  
              
        }
    
    }
    
    Output
    metacharacters D..
    true
    false
    false
    false
    false
    ----------------------------------
    metacharacters D with quantifier....
    true
    
    
    RegexDemo3.java
    import java.util.regex.Pattern;
    
    /**
     * 
     *  . = Any character (may or may not match terminator)
     *
     */
    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(Pattern.matches(".", "m"));//true [char and comes once]
            System.out.println(Pattern.matches(".", "8"));//true [char and comes once]
            System.out.println(Pattern.matches(".", "99"));//false [char but comes more than once]
            System.out.println(Pattern.matches(".", "uiui"));//false [char but comes more than once]
              
        }
    
    }
    
    Output
    true
    true
    false
    false
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_metachar_d.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_metachar_d/?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
  • How to use Regex quantifiers - part 2 | Java Regex | Java Regular Expressions | Regex in java


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

    Click the below Image to Enlarge
    How to use Regex quantifiers - part 2 | Java Regex | Java Regular Expressions | Regex in java
    RegexDemo1.java
    import java.util.regex.Pattern;
    
    
    public class RegexDemo1
    {
        public static void main(String[] args)
        {
            System.out.println("X{n} = X occurs n times only");  
            /*
             * 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(Pattern.matches("a{3}", "aaa"));//true (a should come 3 times)
            System.out.println(Pattern.matches("a{3}", "aaaaaa"));//false (a should come 3 times)
            System.out.println(Pattern.matches("a{3}", "aa"));//false (a should come 3 times) 
            System.out.println(Pattern.matches("a{3}", "a"));//false (a should come 3 times)
            System.out.println(Pattern.matches("a{3}", "bbb"));//false (a should come 3 times)
        }
    
    }
    
    Output
    X{n} = X occurs n times only
    true
    false
    false
    false
    false
    
    
    RegexDemo2.java
    import java.util.regex.Pattern;
    
    public class RegexDemo2
    {
        public static void main(String[] args)
        {
            System.out.println("X{n,} = X occurs n or more times");  
            /*
             * 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(Pattern.matches("a{3,}", "aaa"));//true (a can come 3 times or more)
            System.out.println(Pattern.matches("a{3,}", "aaaaaa"));//true (a can come 3 times or more)
            System.out.println(Pattern.matches("a{3,}", "aa"));//false (a can come 3 times or more) 
            System.out.println(Pattern.matches("a{3,}", "a"));//false (a can come 3 times or more)
            System.out.println(Pattern.matches("a{3,}", "bbb"));//false (a can come 3 times or more)
        }
    
    }
    
    Output
    X{n,} = X occurs n or more times
    true
    true
    false
    false
    false
    
    
    RegexDemo3.java
    import java.util.regex.Pattern;
    
    public class RegexDemo3
    {
        public static void main(String[] args)
        {
            System.out.println("X{y,z} = X occurs at least y times but less than or equal to z times");  
            /*
             * 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(Pattern.matches("a{3,6}", "aaa"));//true (a can come 3 times or <= 6 times)
            System.out.println(Pattern.matches("a{3,6}", "aaaaaa"));//true (a can come 3 times or <= 6 times)
            System.out.println(Pattern.matches("a{3,6}", "aaaaaaaaaaa"));//false (a can come 3 times or <= 6 times)
            System.out.println(Pattern.matches("a{3,6}", "aa"));//false (a can come 3 times or <= 6 times)
            System.out.println(Pattern.matches("a{3,6}", "a"));//false (a can come 3 times or <= 6 times)
            System.out.println(Pattern.matches("a{3,6}", "bbb"));//false (a can come 3 times or <= 6 times)
        }
    
    }
    
    Output
    X{y,z} = X occurs at least y times but less than or equal to z times
    true
    true
    false
    false
    false
    false
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_Quantifier_number_of_times.zip?attredirects=0&d=1

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_Quantifier_number_of_times/?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
  • How to use Regex quantifiers - part 1 | Java Regex | Java Regular Expressions | Regex in java


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

    Click the below Image to Enlarge
    How to use Regex quantifiers - part 1 | Java Regex | Java Regular Expressions | Regex in java
    RegexDemo1.java
    import java.util.regex.Pattern;
    
    
    public class RegexDemo1
    {
        public static void main(String[] args)
        {
            System.out.println("? quantifier = occurs once or not at all");  
            /*
             * 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(Pattern.matches("[ab]?", "a"));//true (a or b comes one time) 
            System.out.println(Pattern.matches("[ab]?", "b"));//true (a or b comes one time)
            System.out.println(Pattern.matches("[ab]?", ""));//true (occurs once or not at all)
            System.out.println(Pattern.matches("[ab]?", "ab"));//false (a and b comes one time)
            System.out.println(Pattern.matches("[ab]?", "aaa"));//false (a comes more than one time)  
            System.out.println(Pattern.matches("[ab]?", "aabbb"));//false (a and b comes more than one time)  
        }
    
    }
    
    Output
    ? quantifier = occurs once or not at all
    true
    true
    true
    false
    false
    false
    
    
    RegexDemo2.java
    import java.util.regex.Pattern;
    
    public class RegexDemo2
    {
        public static void main(String[] args)
        {
            System.out.println("+ quantifier = occurs once or more times");  
            
            /*
             * 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(Pattern.matches("[ab]+", "a"));//true (a or b once or more times)  
            System.out.println(Pattern.matches("[ab]+", "aaa"));//true (a comes more than one time)  
            System.out.println(Pattern.matches("[ab]+", "aabb"));//true (a and b comes more than once)  
            System.out.println(Pattern.matches("[ab]+", "aazzb"));//false (z is not matching pattern)  
        }
    
    }
    
    Output
    + quantifier = occurs once or more times
    true
    true
    true
    false
    
    
    RegexDemo3.java
    import java.util.regex.Pattern;
    
    public class RegexDemo3
    {
        public static void main(String[] args)
        {
            System.out.println("* quantifier = occurs zero or more times");  
            /*
             * 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(Pattern.matches("[ab]*", ""));//true (a or b may come zero or more times)  
            System.out.println(Pattern.matches("[ab]*", "a"));//true (a or b may come zero or more times)
            System.out.println(Pattern.matches("[ab]*", "aaa"));//true (a or b may come zero or more times)
            System.out.println(Pattern.matches("[ab]*", "aaabbb"));//true (a or b may come zero or more times)
            System.out.println(Pattern.matches("[ab]*", "aaabbbx"));//false (x is not matching pattern) 
        }
    
    }
    
    Output
    * quantifier = occurs zero or more times
    true
    true
    true
    true
    false
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_Quantifier_x_question.zip?attredirects=0&d=1

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

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