Monday 18 December 2017

How to use String matcher[is Three letters] in Java Regex | Java Regular Expressions | Regex in java


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

StringMatcher.java

public class StringMatcher
{
    /*
     * returns true if the string contains of three letters
     */

    public boolean isThreeLetters(String s)
    {
        return s.matches("[a-zA-Z]{3}");
    }

    /*
     * returns true if the string does not have a number at the beginning
     */

    public boolean isNoNumberAtBeginning(String s)
    {
        return s.matches("^[^\\d].*");
    }

}

RegexDemo.java

public class RegexDemo
{
    public static void main(String[] args)
    {
        StringMatcher stringMatcher = new StringMatcher();

        System.out.println(stringMatcher.isThreeLetters("cat")); // true
        System.out.println(stringMatcher.isThreeLetters("tiger")); // false

        System.out.println("-----------------------------");

        System.out.println(stringMatcher.isNoNumberAtBeginning("Hello")); // true
        System.out.println(stringMatcher.isNoNumberAtBeginning("9Hello")); // false

    }

}

Output

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

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

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

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