The quantifiers specify the number of occurrences of a character.
RegexDescriptionX?X occurs once or not at allX+X occurs once or more timesX*X occurs zero or more timesX{n}X occurs n times onlyX{n,}X occurs n or more timesX{y,z}X occurs at least y times but less than z timesRegular Expression Character classes and Quantifiers Exampleimport java.util.regex.*; class RegexExample4{ public static void main(String args[]){ System.out.println("? quantifier ...."); System.out.println(Pattern.matches("[amn]?", "a"));//true (a or m or n comes one time) System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a comes more than one time) System.out.println(Pattern.matches("[amn]?", "aammmnn"));//false (a m and n comes more than one time) System.out.println(Pattern.matches("[amn]?", "aazzta"));//false (a comes more than one time) System.out.println(Pattern.matches("[amn]?", "am"));//false (a or m or n must come one time)
System.out.println("+ quantifier ...."); System.out.println(Pattern.matches("[amn]+", "a"));//true (a or m or n once or more times) System.out.println(Pattern.matches("[amn]+", "aaa"));//true (a comes more than one time) System.out.println(Pattern.matches("[amn]+", "aammmnn"));//true (a or m or n comes more than once) System.out.println(Pattern.matches("[amn]+", "aazzta"));//false (z and t are not matching pattern)
System.out.println("* quantifier ...."); System.out.println(Pattern.matches("[amn]*", "ammmna"));//true (a or m or n may come zero or more times)
}} Regex Metacharacters
The regular expression metacharacters work as a short codes.
RegexDescription.Any character (may or may not match terminator)\dAny digits, short of [0-9]\DAny non-digit, short for [^0-9]\sAny whitespace character, short for [\t
\x0B\f\r]\SAny non-whitespace character, short for [^\s]\wAny word character, short for [a-zA-Z_0-9]\WAny non-word character, short for [^\w]\bA word boundary\BA non word boundaryRegular Expression Metacharacters Exampleimport java.util.regex.*; class RegexExample5{ public static void main(String args[]){ System.out.println("metacharacters d....");\\d means digit
System.out.println(Pattern.matches("\\d", "abc"));//false (non-digit) System.out.println(Pattern.matches("\\d", "1"));//true (digit and comes once) System.out.println(Pattern.matches("\\d", "4443"));//false (digit but comes more than once) System.out.println(Pattern.matches("\\d", "323abc"));//false (digit and char)
System.out.println("metacharacters D....");\\D means non-digit
System.out.println(Pattern.matches("\\D", "abc"));//false (non-digit but comes more than once) System.out.println(Pattern.matches("\\D", "1"));//false (digit) System.out.println(Pattern.matches("\\D", "4443"));//false (digit) System.out.println(Pattern.matches("\\D", "323abc"));//false (digit and char) System.out.println(Pattern.matches("\\D", "m"));//true (non-digit and comes once)
System.out.println("metacharacters D with quantifier...."); System.out.println(Pattern.matches("\\D*", "mak"));//true (non-digit and may come 0 or more times)
}} Regular Expression Question 1/*Create a regular expression that accepts alpha numeric characters only. Its length must be 6 characters long only.*/
import java.util.regex.*; class RegexExample6{ public static void main(String args[]){ System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun32"));//true System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "kkvarun32"));//false (more than 6 char) System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "JA2Uk2"));//true System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun$2"));//false ($ is not matched) }} Regular Expression Question 2/*Create a regular expression that accepts 10 digit numeric characters starting with 7, 8 or 9 only.*/
import java.util.regex.*; class RegexExample7{ public static void main(String args[]){ System.out.println("by character classes and quantifiers ..."); System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));//true System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));//true
System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));//false (11 characters) System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));//false (starts from 6) System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));//true
System.out.println("by metacharacters ..."); System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));//true System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));//false (starts from 3)
}} Java Regex Finder Exampleimport java.util.regex.Pattern; import java.util.Scanner; import java.util.regex.Matcher; public class RegexExample8{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while (true) { System.out.println("Enter regex pattern:"); Pattern pattern = Pattern.compile(sc.nextLine()); System.out.println("Enter text:"); Matcher matcher = pattern.matcher(sc.nextLine()); boolean found = false; while (matcher.find()) { System.out.println("I found the text "+matcher.group()+" starting at index "+ matcher.start()+" and ending at index "+matcher.end()); found = true; } if(!found){ System.out.println("No match found."); } } } }
Output:
Enter regex pattern: java Enter text: this is java, do you know java I found the text java starting at index 8 and ending at index 12 I found the text java starting at index 26 and ending at index 30
java regex
Mulai dari awal
