Friday 29 December 2017

How to use predefined character class whitespace and non-whitespace | Java Regex | Regex in java



Click here to watch on Youtube : 
https://www.youtube.com/watch?v=9DuMFjsx0gA&list=UUhwKlOVR041tngjerWxVccw

RegexDemo.java

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

/**
 *
 * Predefined Character Classes
 *
 * The Java regex API also accepts predefined character classes.One
 * special aspect of the Java version of this regex is the escape
 * character.
 *
 * As we will see, most characters will start with a backslash, which
 * has a special meaning in Java. For these to be compiled by the
 * Pattern class – the leading backslash must be escaped i.e. \s
 * becomes \\s.
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        /*
         * Matching white space:
         */

        calculateMatches("\\s", "a    c");
        /*
         * Matching non-white space:
         */

        calculateMatches("\\S", "a    c");
    }

    private static void calculateMatches(String regex, String inputText)
    {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputText);
        int matches = 0;

        /*
         * The find method keeps advancing through the input text and
         * returns true for every match, so we can use it to find the
         * match count as well:
         */

        while (matcher.find())
        {
            ++matches;
        }
        System.out.println("Number of Matches = " + matches);
    }

}

Output

Number of Matches = 4
Number of Matches = 2

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/9ca0aed85cf523c7d53f60f93dd6c7d098a3db63/BasicJava/RegexDemo_predefined_class_s_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
  • How to use predefined character class digit and non-digit | Java Regex | Regex in java


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

    RegexDemo.java

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

    /**
     *
     * Predefined Character Classes
     *
     * The Java regex API also accepts predefined character classes.One
     * special aspect of the Java version of this regex is the escape
     * character.
     *
     * As we will see, most characters will start with a backslash, which
     * has a special meaning in Java. For these to be compiled by the
     * Pattern class – the leading backslash must be escaped i.e. \d
     * becomes \\d.
     *
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            /*
             * Matching digits, equivalent to [0-9]:
             */

            calculateMatches("\\d", "12abcd");
            /*
             * Matching non-digits, equivalent to [^0-9]:
             */

            calculateMatches("\\D", "12abcd");
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 2
    Number of Matches = 4

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/9ca0aed85cf523c7d53f60f93dd6c7d098a3db63/BasicJava/RegexDemo_predefined_class_d_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 Metacharacter subtraction class | Java Regex | Java Regular Expressions | Regex in java


    Click here to watch on Youtube : 
    https://www.youtube.com/watch?v=8rW3y46GkN8&list=UUhwKlOVR041tngjerWxVccw

    RegexDemo.java

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

    /**
     *
     * Subtraction Class
     *
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            /*
             * We will get 8 matches because the Subtraction of the two
             * sets has only 8 elements.
             */

            calculateMatches("[0-9&&[^12]]", "0123456789");
            calculateMatches("[0-9&&[^12]]", "123");
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 8
    Number of Matches = 1

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/9ca0aed85cf523c7d53f60f93dd6c7d098a3db63/BasicJava/RegexDemo_substraction_class/?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 Metacharacter Intersection class | Java Regex | Java Regular Expressions | Regex in java


    Click here to watch on Youtube :
    https://www.youtube.com/watch?v=7H4yAVbg3-Y&list=UUhwKlOVR041tngjerWxVccw

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

    /**
     *
     * Intersection Class
     *
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            /*
             * We will get 4 matches because the intersection of the two
             * sets has only 4 elements[3456].
             */

            calculateMatches("[1-6&&[3-9]]", "3456");
            calculateMatches("[1-6&&[3-9]]", "123456789");
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 4
    Number of Matches = 4

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/9ca0aed85cf523c7d53f60f93dd6c7d098a3db63/BasicJava/RegexDemo_intersection_class/?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 26 December 2017

    How to use Metacharacter Union class | Java Regex | Java Regular Expressions | Regex in java


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

    RegexDemo.java

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

    /**
     *
     * Union Class
     *
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            /*
             * This will only match 6 out of the 9 integers because the
             * union set skips  4,5 and 6.
             */

            calculateMatches("[1-3[7-9]]", "123456789");
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 6

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_union_class/?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 Metacharacter Range class | Java Regex | Java Regular Expressions | Regex in java


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

    RegexDemo.java

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    /**
     *
     * Range Class Example
     *
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            /*
             * Matching uppercase letters:
             */

            calculateMatches( "[A-Z]", "Welcome To India");
            /*
             * Matching lowercase letters:
             */

            calculateMatches( "[a-z]", "Two");
            /*
             * Matching both upper case and lower case letters:
             */

            calculateMatches("[a-zA-Z]", "Two");
            /*
             * Matching a given range of numbers:
             */

            calculateMatches( "[1-5]", "hi 65");
           
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 3
    Number of Matches = 2
    Number of Matches = 3
    Number of Matches = 1

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_metachar_range_class/?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 Metacharacter NOR class | Java Regex | Java Regular Expressions | Regex in java


    Click here to watch on Youtube : 
    https://www.youtube.com/watch?v=3Sc1buVwCXg&list=UUhwKlOVR041tngjerWxVccw

    RegexDemo.java

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    /**
     *
     * NOR Class Example
     *
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            calculateMatches("[^abc]", "z");
            calculateMatches("[^bcr]at", "sat mat");
            calculateMatches("[^bcr]at", "sat mat eat bat cat");
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 1
    Number of Matches = 2
    Number of Matches = 3

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_metachar_nor_class/?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 metacharacter OR class | Java Regex | Java Regular Expressions | Regex in java


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

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

    /**
     * OR Class Example
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            /*
             * [abc] - Any of the elements in the set is matched
             */

            calculateMatches("[abc]", "b");
            calculateMatches("[abc]", "cab");
            calculateMatches("[abc]", "caABZ9");

            /*
             * They can also be alternated as part of a String. In the
             * following example, when we create different words by
             * alternating the first letter with each element of the set,
             * they are all matched:
             */

            calculateMatches("[bcr]at", "bat cat rat zat");

        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 1
    Number of Matches = 3
    Number of Matches = 2
    Number of Matches = 3

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_metachar_or_class/?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 Metacharacter dot | Java Regex | Java Regular Expressions | Regex in java


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

    RegexDemo.java

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

    /**
     * Meta Characters example
     */

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            /*
             * Meta characters affect the way a pattern is matched, in a
             * way adding logic to the search pattern. The Java API
             * supports several metacharacters, the most straightforward
             * being the dot “." which matches any character:
             */

            calculateMatches(".", "foofoo");
            /*
             * Notice the dot after the foo in the regex. The matcher
             * matches every text that is preceded by foo since the last
             * dot part means any character after. So after finding the
             * first foo, the rest is seen as any character. That is why
             * there is only a single match.
             */

            calculateMatches("foo.", "foofoo");
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = " + matches);
        }

    }

    Output

    Number of Matches = 6
    Number of Matches = 1

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_metachar_dot_cal/?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 calculate how many times foo is there in the input text | Regex in java


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

    RegexDemo.java

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

    public class RegexDemo
    {

        public static void main(String[] args)
        {
            calculateMatches("foo", "foo come foo");
        }

        private static void calculateMatches(String regex, String inputText)
        {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(inputText);
            int matches = 0;

            /*
             * The find method keeps advancing through the input text and
             * returns true for every match, so we can use it to find the
             * match count as well:
             */

            while (matcher.find())
            {
                ++matches;
            }
            System.out.println("Number of Matches = "+matches);
        }

    }

    Output

    Number of Matches = 2

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_calculate_matches/?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 filter list of email addresses using asPredicate method of Pattern class | Regex in java


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

    RegexPredicateDemo.java

    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Predicate;
    import java.util.regex.Pattern;
    import java.util.stream.Collectors;

    public class RegexPredicateDemo
    {

        public static void main(String[] args)
        {
            String regex = "^(.+)@gmail.com$";
            Pattern pattern = Pattern.compile(regex);

            /*
             * Returns:The predicate which can be used for matching on a
             * string
             */

            Predicate<String> emailFilterPredicate = pattern.asPredicate();

            List<String> emailList = Arrays.asList("peter@yahoo.com",
                    "bob@gmail.com", "juli@gmail.com",
                    "david@rediff.com");

            /*
             *  Apply predicate filter
             */

            List<String> desiredEmailList = emailList.stream()
                                               .filter(emailFilterPredicate)
                                               .collect(Collectors.<String>toList());

            desiredEmailList.forEach(System.out::println);
        }

    }

    Output

    bob@gmail.com
    juli@gmail.com

    RegexDemo.java

    import java.util.Arrays;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class RegexDemo
    {

        public static void main(String[] args)
        {

            String regex = "^(.+)@gmail.com$";
            Pattern pattern = Pattern.compile(regex);

            List<String> emailList = Arrays.asList("peter@yahoo.com",
                    "bob@gmail.com", "juli@gmail.com",
                    "david@rediff.com");

            for (String email : emailList)
            {
                Matcher matcher = pattern.matcher(email);

                if (matcher.matches())
                {
                    System.out.println(email);
                }
            }
        }

    }

    Output

    bob@gmail.com
    juli@gmail.com

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_asPredicate/?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