- Import
java.util.regex
package as Java doesn’t have a built-in regex class - Included classes:
Pattern
class- Defines a pattern to be used in a search
Matcher
class- Used to search for pattern
PatternSyntaxException
class- Indicates syntax error in regex pattern
Pattern pattern = Pattern.compile(/\W+/g, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("This is cool");
boolean matchFound = matcher.find();
Pattern.compile(regex).matcher(str).replaceAll(replacement)