Match Fixed length
It is also possible to designate a sequence of fixed length. For example, to specify four-digit numbers, we write [0-9]{4}. The number in the braces { and } denotes the number of repetitions. We can specify the minimum and maximum numbers of repetitions also. Here are the rules:
Program One
Program Two
Program Three
import java.util.regex.Pattern; import java.util.regex.Matcher; public class Javaapp { public static void main(String[] args) { Pattern pat = Pattern.compile("[0-9]{3}"); Matcher mat = pat.matcher("22 333 4444 55555 666666"); int i = 0; while(mat.find()) { i++; System.out.println(i+"th subsequence : "+mat.group()); } } }
Program Two Source
import java.util.regex.Pattern; import java.util.regex.Matcher; public class Javaapp { public static void main(String[] args) { Pattern pat = Pattern.compile("[0-9]{3,}"); Matcher mat = pat.matcher("22 333 4444 55555 666666"); int i = 0; while(mat.find()) { i++; System.out.println(i+"th subsequence : "+mat.group()); } } }
Program Three Source
import java.util.regex.Pattern; import java.util.regex.Matcher; public class Javaapp { public static void main(String[] args) { Pattern pat = Pattern.compile("[0-9]{3,5}"); Matcher mat = pat.matcher("22 333 4444 55555 666666"); int i = 0; while(mat.find()) { i++; System.out.println(i+"th subsequence : "+mat.group()); } } }