Tuesday, 28 November 2017

How to use any character, digit and non-digit Regex meta-characters | Java Regex


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

Click the below Image to Enlarge
How to use any character, digit and non-digit Regex meta-characters | Java Regex
RegexDemo1.java
import java.util.regex.Pattern;

/**
 * 
 * \d = Any digits, short of [0-9]
 *
 */

public class RegexDemo1
{
    public static void main(String[] args)
    {
        System.out.println("metacharacters d");
        
        /*
         * Parameters:
         * 
         * regex - The expression to be compiled
         * 
         * input - The character sequence to be matched
         * 
         * Returns:
         * 
         * whether or not the regular expression matches on the input
         * 
         */
        
        System.out.println(Pattern.matches("\\d", "9"));//true (digit and comes once)
        System.out.println(Pattern.matches("\\d", "dfg"));//false (non-digit)  
        System.out.println(Pattern.matches("\\d", "8888"));//false (digit but comes more than once)  
        System.out.println(Pattern.matches("\\d", "555abc"));//false (digit and char)  
        
        System.out.println("----------------------------------");
        
        System.out.println("metacharacters d with quantifier....");  
        System.out.println(Pattern.matches("\\d*", "56565"));//true (digit may come 0 or more times)
    }

}
Output
metacharacters d
true
false
false
false
----------------------------------
metacharacters d with quantifier....
true

RegexDemo2.java
import java.util.regex.Pattern;

/**
 * 
 * \D = Any non-digit, short for [^0-9]
 *
 */
public class RegexDemo2
{
    public static void main(String[] args)
    {
        System.out.println("metacharacters D..");  
        /*
         * Parameters:
         * 
         * regex - The expression to be compiled
         * 
         * input - The character sequence to be matched
         * 
         * Returns:
         * 
         * whether or not the regular expression matches on the input
         * 
         */
        System.out.println(Pattern.matches("\\D", "m"));//true (non-digit and comes once)
        System.out.println(Pattern.matches("\\D", "xds"));//false (non-digit but comes more than once)  
        System.out.println(Pattern.matches("\\D", "1"));//false (digit)  
        System.out.println(Pattern.matches("\\D", "8989"));//false (digit)  
        System.out.println(Pattern.matches("\\D", "121abc"));//false (digit and char)  
        
        System.out.println("----------------------------------");
        
        System.out.println("metacharacters D with quantifier....");  
        System.out.println(Pattern.matches("\\D*", "peter"));//true (non-digit and may come 0 or more times)  
          
    }

}
Output
metacharacters D..
true
false
false
false
false
----------------------------------
metacharacters D with quantifier....
true

RegexDemo3.java
import java.util.regex.Pattern;

/**
 * 
 *  . = Any character (may or may not match terminator)
 *
 */
public class RegexDemo3
{
    public static void main(String[] args)
    {
        /*
         * Parameters:
         * 
         * regex - The expression to be compiled
         * 
         * input - The character sequence to be matched
         * 
         * Returns:
         * 
         * whether or not the regular expression matches on the input
         * 
         */
        System.out.println(Pattern.matches(".", "m"));//true [char and comes once]
        System.out.println(Pattern.matches(".", "8"));//true [char and comes once]
        System.out.println(Pattern.matches(".", "99"));//false [char but comes more than once]
        System.out.println(Pattern.matches(".", "uiui"));//false [char but comes more than once]
          
    }

}
Output
true
true
false
false

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

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

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