Regular Expression

Theory

About Regular Expression:
Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating and editing a text. It is widely used to define constraint on strings such as password. Regular Expressions are provided under java.util.regex package.

The Java Regex or Regular Expression is an API to define a pattern for searching or manipulating strings.It is widely used to define the constraint on strings such as password and email validation. After learning Java regex tutorial, you will be able to test your regular expressions by the Java Regex Tester Tool. Java Regex API provides 1 interface and 3 classes in java.util.regex package.

Java Regex API



The Matcher and Pattern classes provide the facility of Java regular expression. The java.util.regex package provides following classes and interfaces for regular expressions.

java.util.regex package:

  1. MatchResult interface
  2. Matcher class
  3. Pattern class
  4. PatternSyntaxException class


Java Regex API



Matcher class

No.     Method              Description
1boolean matches()  test whether the regular expression matches the pattern.
2boolean find()  finds the next expression that matches the pattern.
3boolean find(int start)  finds the next expression that matches the pattern from the given start number.
4String group()  returns the matched subsequence.
5int start()  returns the starting index of the matched subsequence.
6int end()  returns the ending index of the matched subsequence.
7int groupCount()  returns the total number of the matched subsequence.




Java Regex API

Pattern class

It is the compiled version of a regular expression. It is used to define a pattern for the regex engine.

No.   Method         Description
1static Pattern compile(String regex)   compiles the given regex and returns the instance of the Pattern.
2Matcher matcher(CharSequence input)   creates a matcher that matches the given input with the pattern.
3static boolean matches(String regex, CharSequence input)   It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with    the pattern.
4String[] split(CharSequence input)   splits the given input string around matches of given pattern.
5String pattern()   returns the regex pattern.




Java Regex API

Regex Quantifiers

The quantifiers specify the number of occurrences of a character.

Regex          Description
X?   X occurs once or not at all
X+   X occurs once or more times
X*   X occurs zero or more times
X{n}   X occurs n times only
X{n,}   X occurs n or more times
X{y,z}   X occurs at least y times but less than z times




Regex Metacharacters

The regular expression metacharacters work as shortcodes.

Regex      Description
.   Any character (may or may not match terminator)
\d   Any digits, short of [0-9]
\D   Any non-digit, short for [^0-9]
\s   Any whitespace character, short for [\t\n\x0B\f\r]
\S   Any non-whitespace character, short for [^\s]
\w   Any word character, short for [a-zA-Z_0-9]
\W   Any non-word character, short for [^\w]
\b   A word boundary
\B   A non word boundary