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 |
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 = falseRegexDemo2.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 = falseClick 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:
No comments:
Post a Comment