import java.util.regex.*; public class RegexExample { public static void main(String[] args) { String regex = "java"; Pattern pattern = Pattern.compile(regex); String input = "I love programming in java"; Matcher matcher = pattern.matcher(input); if (matcher.find()) { System.out.println("Match found"); } else { System.out.println("Match not found"); } } }
import java.util.regex.*; public class PhoneRegexExample { public static void main(String[] args) { String regex = "\\d{3}-\\d{3}-\\d{4}"; Pattern pattern = Pattern.compile(regex); String input = "My phone number is 555-123-4567"; Matcher matcher = pattern.matcher(input); if (matcher.find()) { System.out.println("Phone number found"); } else { System.out.println("Phone number not found"); } } }This example compiles a regular expression pattern that matches a phone number in the format of "XXX-XXX-XXXX". It then uses a matcher to search for this pattern in the input string "My phone number is 555-123-4567". Since the pattern is found, it prints "Phone number found". This example also uses the java.util.regex package. In conclusion, java.util.regex is the package library that provides Pattern compile for working with regular expressions in Java.