Click here to watch in Youtube :
https://www.youtube.com/watch?v=BSuBDTSCV0A&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
How to use Regex quantifiers - part 2 | Java Regex | Java Regular Expressions | Regex in java |
import java.util.regex.Pattern; public class RegexDemo1 { public static void main(String[] args) { System.out.println("X{n} = X occurs n times only"); /* * 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("a{3}", "aaa"));//true (a should come 3 times) System.out.println(Pattern.matches("a{3}", "aaaaaa"));//false (a should come 3 times) System.out.println(Pattern.matches("a{3}", "aa"));//false (a should come 3 times) System.out.println(Pattern.matches("a{3}", "a"));//false (a should come 3 times) System.out.println(Pattern.matches("a{3}", "bbb"));//false (a should come 3 times) } }Output
X{n} = X occurs n times only true false false false falseRegexDemo2.java
import java.util.regex.Pattern; public class RegexDemo2 { public static void main(String[] args) { System.out.println("X{n,} = X occurs n or more times"); /* * 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("a{3,}", "aaa"));//true (a can come 3 times or more) System.out.println(Pattern.matches("a{3,}", "aaaaaa"));//true (a can come 3 times or more) System.out.println(Pattern.matches("a{3,}", "aa"));//false (a can come 3 times or more) System.out.println(Pattern.matches("a{3,}", "a"));//false (a can come 3 times or more) System.out.println(Pattern.matches("a{3,}", "bbb"));//false (a can come 3 times or more) } }Output
X{n,} = X occurs n or more times true true false false falseRegexDemo3.java
import java.util.regex.Pattern; public class RegexDemo3 { public static void main(String[] args) { System.out.println("X{y,z} = X occurs at least y times but less than or equal to z times"); /* * 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("a{3,6}", "aaa"));//true (a can come 3 times or <= 6 times) System.out.println(Pattern.matches("a{3,6}", "aaaaaa"));//true (a can come 3 times or <= 6 times) System.out.println(Pattern.matches("a{3,6}", "aaaaaaaaaaa"));//false (a can come 3 times or <= 6 times) System.out.println(Pattern.matches("a{3,6}", "aa"));//false (a can come 3 times or <= 6 times) System.out.println(Pattern.matches("a{3,6}", "a"));//false (a can come 3 times or <= 6 times) System.out.println(Pattern.matches("a{3,6}", "bbb"));//false (a can come 3 times or <= 6 times) } }Output
X{y,z} = X occurs at least y times but less than or equal to z times true true false false false falseClick the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_Quantifier_number_of_times.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_Quantifier_number_of_times
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_Quantifier_number_of_times/?at=master
See also:
No comments:
Post a Comment