Wednesday, 3 January 2018

How to use Quantifiers a{2,3} | Java Regex | Java Regular Expressions | Regex in java


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

RegexDemo.java

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

/**
 *
 * Quantifiers
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {

        /*
         * We’ve specified at least two occurrences but not exceeding
         * three.
         */

        isMatch("a{2,3}", "aa");
        isMatch("a{2,3}", "aaa");
        isMatch("a{2,3}", "a");
        isMatch("a{2,3}", "aaaaaa");

    }

    private static void isMatch(String regex, String inputText)
    {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputText);

        System.out.println("Regex = " + regex + " , " + "InputText = "
                + inputText + " is matching? = " + matcher.matches());
    }

}

Output

Regex = a{2,3} , InputText = aa is matching? = true
Regex = a{2,3} , InputText = aaa is matching? = true
Regex = a{2,3} , InputText = a is matching? = false
Regex = a{2,3} , InputText = aaaaaa is matching? = false

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

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

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