Friday 1 December 2017

What are Regex quantifiers and examples | Java Regex | Java Regular Expressions | Regex in java


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

Click the below Image to Enlarge
What are Regex quantifiers and examples | Java Regex | Java Regular Expressions | Regex in java
RegexDemo1.java
import java.util.regex.Pattern;


public class RegexDemo1
{
    public static void main(String[] args)
    {
        /*
         *  * = Occurs zero or more times, is short for {0,}
         */
        System.out.println(Pattern.matches("X*", "")); // true
        System.out.println(Pattern.matches("X*", "XXXX")); // true
        System.out.println(Pattern.matches("X*", "aaaa")); // false
        
        System.out.println("-------------------------------");
        
        /*
         *  + = Occurs one or more times, is short for {1,}
         */
        System.out.println(Pattern.matches("X+", "")); // false
        System.out.println(Pattern.matches("X+", "X")); // true
        System.out.println(Pattern.matches("X+", "XXXX")); // true
        System.out.println(Pattern.matches("X+", "BBBB")); // false
        
        System.out.println("-------------------------------");
        
        /*
         *  ? = Occurs no or one times, ? is short for {0,1}.
         */
        System.out.println(Pattern.matches("X?", "")); // true
        System.out.println(Pattern.matches("X?", "X")); // true
        System.out.println(Pattern.matches("X?", "XXXX")); // false
        System.out.println(Pattern.matches("X?", "BBBB")); // false     
        
    }

}
Output
true
true
false
-------------------------------
false
true
true
false
-------------------------------
true
true
false
false

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


public class RegexDemo2
{
    public static void main(String[] args)
    {
        /*
         *  {X} = Occurs X number of times, {} 
         *  describes the order of the preceding liberal
         */
        System.out.println(Pattern.matches("X{3}", "X")); // false
        System.out.println(Pattern.matches("X{3}", "XXX")); // true
        System.out.println(Pattern.matches("X{3}", "aaaa")); // false
        
        System.out.println("-------------------------------");
        
        System.out.println(Pattern.matches("\\d{3}", "989")); // true
        System.out.println(Pattern.matches("\\d{3}", "9898")); // false
        
        System.out.println("-------------------------------");
        
        System.out.println(Pattern.matches(".{10}", "99999aaaaa")); // true
        System.out.println(Pattern.matches(".{10}", "99999")); // false
        
                
    }

}
Output
false
true
false
-------------------------------
true
false
-------------------------------
true
false

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


public class RegexDemo3
{
    public static void main(String[] args)
    {
        /*
         *  {X,Y} = Occurs between X and Y times
         */
        System.out.println(Pattern.matches("\\d{1,4}", "9")); // true
        System.out.println(Pattern.matches("\\d{1,4}", "98")); // true
        System.out.println(Pattern.matches("\\d{1,4}", "9888")); // true
        System.out.println(Pattern.matches("\\d{1,4}", "9898909")); // false
    }

}
Output
true
true
true
false

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

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

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