Thursday, 30 November 2017

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


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

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

/**
 * 
 * \s = Any whitespace character should come only one time, short for [\t\n\x0B\f\r]
 *
 */

public class RegexDemo1
{
    public static void main(String[] args)
    {
        System.out.println("metacharacters s");
        /*
         * 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("\\s", " "));//true [Only one whitespace can come]
        System.out.println(Pattern.matches("\\s", "\t"));//true [Only one whitespace can come]
        System.out.println(Pattern.matches("\\s", "\n"));//true [Only one whitespace can come]
        System.out.println(Pattern.matches("\\s", "\f"));//true [Only one whitespace can come]
        System.out.println(Pattern.matches("\\s", "     "));//false [Only one whitespace can come, but more whitespaces]
        System.out.println(Pattern.matches("\\s", "\t\t"));//false [Only one whitespace can come, but more whitespaces]
        System.out.println(Pattern.matches("\\s", "ab"));//false [not a whitespace]
        
        System.out.println("----------------------------------");
        
        System.out.println("metacharacters s with quantifier....");  
        System.out.println(Pattern.matches("\\s*", "       "));//true [more than one whitespace can come]
        System.out.println(Pattern.matches("\\s*", "\r\r"));//true [more than one whitespace can come]
    }

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

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

/**
 * 
 * \S = Any non-whitespace character Should come only one time, short for [^\s]
 *
 */

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

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

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

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

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