Tuesday 28 November 2017

How to use range and union Regex character classes | Java Regex


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

Click the below Image to Enlarge
How to use range and union Regex character classes | Java Regex
RegexDemo1.java
import java.util.regex.Pattern;

/**
 * 
 * [a-zA-Z] : Means any character that IS a-z OR A-Z
 *
 */
public class RegexDemo1
{
    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("[a-zA-Z] matches A = " +   Pattern.matches("[a-zA-Z]", "A"));
        System.out.println("[a-zA-Z] matches d = " +   Pattern.matches("[a-zA-Z]", "d"));
        System.out.println("[a-zA-Z] matches ddZY = " +   Pattern.matches("[a-zA-Z]", "ddZY"));
        System.out.println("[a-zA-Z] matches 1 = " +   Pattern.matches("[a-zA-Z]", "1"));
    }

}
Output
[a-zA-Z] matches A = true
[a-zA-Z] matches d = true
[a-zA-Z] matches ddZY = false
[a-zA-Z] matches 1 = false

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

/**
 * 
 * [a-zA-Z] = Means any character that IS a-d OR m-p.
 *
 */
public class RegexDemo2
{
    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("[a-d[m-p]] matches a = " +   Pattern.matches("[a-d[m-p]]", "a"));
        System.out.println("[a-d[m-p]] matches n = " +   Pattern.matches("[a-d[m-p]]", "n"));
        System.out.println("[a-d[m-p]] matches e = " +   Pattern.matches("[a-d[m-p]]", "e"));
        System.out.println("[a-d[m-p]] matches A = " +   Pattern.matches("[a-d[m-p]]", "A"));
        System.out.println("[a-d[m-p]] matches am = " +   Pattern.matches("[a-d[m-p]]", "am"));
    }

}
Output
[a-d[m-p]] matches a = true
[a-d[m-p]] matches n = true
[a-d[m-p]] matches e = false
[a-d[m-p]] matches A = false
[a-d[m-p]] matches am = false

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

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

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