Friday 5 January 2018

How to capturing the groups in Regex | Java Regex | Java Regular Expressions | Regex in java


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

RegexDemo.java

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

/**
 *
 * Capturing Groups
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {

        /*
         * The API also allows us to treat multiple digits as a
         * single unit through capturing groups.
         */

        isMatch("(\\d\\d)", "12");
        isMatch("(\\d\\d)", "123");
        isMatch("(\\d\\d)", "a2");

    }

    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 = (\d\d) , InputText = 12 is matching? = true
Regex = (\d\d) , InputText = 123 is matching? = false
Regex = (\d\d) , InputText = a2 is matching? = false

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/770448188f0b97ce4037665f1b7f608c5df70664/BasicJava/RegexDemo_capture_group/?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