Monday, 25 December 2017

How to compare the text with input text in a case-insensitive way | Regex in java


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

RegexDemo.java

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

public class RegexDemo
{

    public static void main(String[] args)
    {
        String inputCharSeq = "Hello peter";
        String regex = "HELLO PETER";

        /*
         * Parameters:
         *
         * regex - The expression to be compiled
         *
         * flags - Match flags, a bit mask that may include
         * CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE,
         * CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and
         * COMMENTS
         *
         * Returns:
         *
         * the given regular expression compiled into a pattern with
         * the given flags
         *
         */

        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputCharSeq);

        System.out.println("CASE_INSENSITIVE matches   = " + matcher.matches());

        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(inputCharSeq);

        System.out.println("CASE_SENSITIVE matches   = " + matcher.matches());
    }

}

Output

CASE_INSENSITIVE matches   = true
CASE_SENSITIVE matches   = false

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_case_insensitive/?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
  • 1 comment: