Regular Expression In Java Example
Chapter:
Regular Expression
Last Updated:
09-07-2016 05:23:28 UTC
Program:
/* ............... START ............... */
import java.util.regex.Pattern;
public class JavaRegexExample {
public static void main(String args[]) {
boolean isMatch;
String pattern;
String str = " Java Regex example - Java Scan";
pattern = ".*Scan*";
isMatch = Pattern.matches(pattern, str);
System.out.println("Result 1: " + isMatch);
pattern = ".*scan*";
isMatch = Pattern.matches(pattern, str);
System.out.println("Result 2: " + isMatch);
}
}
/* ............... END ............... */
Output
Result 1: true
Result 2: false
Notes:
-
A regular expression (regex or regexp for short) is a special text string for describing a search pattern.
- Regular Expression can be used to search, edit or manipulate text.
- Java Regex classes are present in java.util.regex package that contains three classes which is described below.
- Pattern: Pattern object is the compiled version of the regular expression.
- Matcher: Matcher is the java regex engine object that matches the input String pattern with the pattern object created.
- PatternSyntaxException: PatternSyntaxException is thrown if the regular expression syntax is not correct.
Tags
Java Regular Expression