Click here to watch in Youtube :
https://www.youtube.com/watch?v=fQaqYMCKV3k&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
What is Regex character classes and how to use simple class and negation | Java Regex |
import java.util.regex.Pattern; /** * * [abc] = if a or b or c returns true else false. * */ 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("[abc] matches a = " + Pattern.matches("[abc]", "a")); System.out.println("[abc] matches b = " + Pattern.matches("[abc]", "b")); System.out.println("[abc] matches c = " + Pattern.matches("[abc]", "c")); System.out.println("[abc] matches z = " + Pattern.matches("[abc]", "z")); System.out.println("[abc] matches aa = " + Pattern.matches("[abc]", "aa")); System.out.println("[abc] matches abc = " + Pattern.matches("[abc]", "abc")); } }Output
[abc] matches a = true [abc] matches b = true [abc] matches c = true [abc] matches z = false [abc] matches aa = false [abc] matches abc = falseRegexDemo2.java
import java.util.regex.Pattern; /** * * [^abc] = if any character except a or b or c returns true else false. * */ 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("[^abc] matches a = " + Pattern.matches("[^abc]", "a")); System.out.println("[^abc] matches b = " + Pattern.matches("[^abc]", "b")); System.out.println("[^abc] matches c = " + Pattern.matches("[^abc]", "c")); System.out.println("[^abc] matches z = " + Pattern.matches("[^abc]", "z")); System.out.println("[^abc] matches zz = " + Pattern.matches("[^abc]", "zz")); System.out.println("[^abc] matches zzz = " + Pattern.matches("[^abc]", "zzz")); } }Output
[^abc] matches a = false [^abc] matches b = false [^abc] matches c = false [^abc] matches z = true [^abc] matches zz = false [^abc] matches zzz = falseClick the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_char_class_abc.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_char_class_abc
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_char_class_abc/?at=master
See also:
No comments:
Post a Comment