Tuesday 9 January 2018

How to use comments flag | Java Regex | Java Regular Expressions | Regex in java


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

RegexDemo.java

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

/**
 *
 * Flags
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        /*
         * Pattern.COMMENTS
         *
         * when we use the flag, it will ignore the extra spaces and
         * the every text starting with # will be seen as a comment to
         * be ignored for each line:
         */

        calculateMatches("dog$  #check end of text", "This is a dog",
                Pattern.COMMENTS);
    }

    private static void calculateMatches(String regex, String inputText,
            int flag)
    {
        Pattern pattern = Pattern.compile(regex, flag);
        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

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

Click the below link to view the code in Github:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_comments_flag

Click the below link to view the code in Bitbucket:
https://bitbucket.org/ramram43210/java/src/07583a7ceb95d30e4d41a36362b049aa5d1ec520/BasicJava/RegexDemo_comments_flag/?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