Tuesday 19 December 2017

How to check string contains abc, string does not start with number and string contains 3 letters


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

RegexDemo.java

import java.util.regex.Pattern;

public class RegexDemo
{

    public static void main(String[] args)
    {
        /*
         * returns true if the string contains "abc" at any place
         */

        System.out.println(Pattern.matches(".*abc.*", "deabcpq"));// True
       
        System.out.println("------------------------------------");

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

        System.out.println(Pattern.matches("^[^\\d].*", "123abc")); // False
        System.out.println(Pattern.matches("^[^\\d].*", "abc123")); // True
       
        System.out.println("------------------------------------");

        // returns true if the string contains of three letters
        System.out.println(Pattern.matches("[a-zA-Z][a-zA-Z][a-zA-Z]", "aPz"));// True
        System.out.println(Pattern.matches("[a-zA-Z][a-zA-Z][a-zA-Z]", "aAA"));// True
        System.out.println(Pattern.matches("[a-zA-Z][a-zA-Z][a-zA-Z]", "apZx"));// False

    }

}

Output

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

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

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

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