Thursday 30 November 2017

How to use word and non-word Regex meta-characters | Java Regex


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

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

/**
 * 
 * \w = Any word character, short for [a-zA-Z_0-9]
 *
 */

public class RegexDemo1
{
    public static void main(String[] args)
    {
        System.out.println("metacharacters w");
        /*
         * 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("\\w", "a"));//true
        System.out.println(Pattern.matches("\\w", "9"));//true
        System.out.println(Pattern.matches("\\w", "_"));//true
        System.out.println(Pattern.matches("\\w", "a9"));//false
        
        System.out.println("----------------------------------");
        
        System.out.println("metacharacters w with quantifier....");  
        System.out.println(Pattern.matches("\\w*", "peter_99"));//true
        System.out.println(Pattern.matches("\\w*", "peter_99@"));//false
    }

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

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

/**
 * 
 * \W = Any non-word character, short for [^\w]
 *
 */

public class RegexDemo2
{
    public static void main(String[] args)
    {
        System.out.println("metacharacters W");
        /*
         * 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("\\W", "@"));//true
        System.out.println(Pattern.matches("\\W", "a"));//false
        System.out.println(Pattern.matches("\\W", "9"));//false
        System.out.println(Pattern.matches("\\W", "@@"));//false
        
        System.out.println("----------------------------------");
        
        System.out.println("metacharacters W with quantifier....");  
        System.out.println(Pattern.matches("\\W*", "@@"));//true
        System.out.println(Pattern.matches("\\W*", "@@peter"));//false
    }

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

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

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

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