Tuesday 19 December 2017

How to check string matches exactly another string | Java Regex | Regex in java


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

RegexDemo.java

import java.util.regex.Pattern;

public class RegexDemo
{

    public static void main(String[] args)
    {
        /*
         *  It would return true if string matches exactly "Tom"
         */

        System.out.println(Pattern.matches("Tom", "Tom")); // true
        System.out.println(Pattern.matches("Tom", "tom")); // false
       
        System.out.println("-----------------------------");

        /*
         * returns true if the string matches exactly "tom" or "Tom"
         */

        System.out.println(Pattern.matches("[Tt]om", "Tom")); // true
        System.out.println(Pattern.matches("[Tt]om", "tom")); // true
        System.out.println(Pattern.matches("[Tt]om", "Tome")); // false

        System.out.println("-----------------------------");
       
        /*
         * Returns true if the string matches exactly "tim" or "Tim" or "jin" or
         * "Jin"
         */

        System.out.println(Pattern.matches("[tT]im|[jJ]in", "Tim"));// true
        System.out.println(Pattern.matches("[tT]im|[jJ]in", "jin"));// true
        System.out.println(Pattern.matches("[tT]im|[jJ]in", "john"));// false

    }

}

Output

true
false
-----------------------------
true
true
false
-----------------------------
true
true
false

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

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

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