Click here to watch in Youtube :
https://www.youtube.com/watch?v=A7KukERQTZI&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
How to use intersection and subtraction Regex character classes | Java Regex |
import java.util.regex.Pattern; /** * * [a-z&&[def]] = d, e, or f (intersection) * */ 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-z&&[def]] matches d = " + Pattern.matches("[a-z&&[def]]", "d")); System.out.println("[a-z&&[def]] matches e = " + Pattern.matches("[a-z&&[def]]", "e")); System.out.println("[a-z&&[def]] matches f = " + Pattern.matches("[a-z&&[def]]", "f")); System.out.println("[a-z&&[def]] matches z = " + Pattern.matches("[a-z&&[def]]", "z")); System.out.println("[a-z&&[def]] matches def = " + Pattern.matches("[a-z&&[def]]", "def")); } }Output
[a-z&&[def]] matches d = true [a-z&&[def]] matches e = true [a-z&&[def]] matches f = true [a-z&&[def]] matches z = false [a-z&&[def]] matches def = falseRegexDemo2.java
import java.util.regex.Pattern; /** * * [a-z&&[^bc]] = a through z, except for b and c * */ 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-z&&[^bc]] matches h = " + Pattern.matches("[a-z&&[^bc]]", "h")); System.out.println("[a-z&&[^bc]] matches c = " + Pattern.matches("[a-z&&[^bc]]", "c")); System.out.println("[a-z&&[^bc]] matches hh = " + Pattern.matches("[a-z&&[^bc]]", "hh")); } }Output
[a-z&&[^bc]] matches h = true [a-z&&[^bc]] matches c = false [a-z&&[^bc]] matches hh = falseRegexDemo3.java
import java.util.regex.Pattern; /** * * [a-z&&[^m-p]] = a through z, and not m through p * */ public class RegexDemo3 { 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-z&&[^m-p]] matches b = " + Pattern.matches("[a-z&&[^m-p]]", "b")); System.out.println("[a-z&&[^m-p]] matches o = " + Pattern.matches("[a-z&&[^m-p]]", "o")); System.out.println("[a-z&&[^m-p]] matches bb = " + Pattern.matches("[a-z&&[^m-p]]", "bb")); } }Output
[a-z&&[^m-p]] matches b = true [a-z&&[^m-p]] matches o = false [a-z&&[^m-p]] matches bb = falseClick the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_char_class_intersection.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_char_class_intersection
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_char_class_intersection/?at=master
See also:
online training on java
ReplyDeletejava online training hyderabad
Thankyou for helping out, great info.
ReplyDeletejava online classes
full stack java developer course