Tuesday, 28 November 2017

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
  • No comments:

    Post a Comment