Regular expressions

The Pattern class represents a regular expression pattern.

The Matcher class represents an engine that performs match operations.

The String contains some methods that work with regular expressions.

String src = "123 23 45 67";

Pattern[] patterns = {
    Pattern.compile("\\d+"), // integer numbers
    Pattern.compile("\\s+")  // white spaces
   };

Matcher matcher = patterns[0].matcher(src);
matcher.region(0, src.length());

boolean foundMatch = false; while (matcher.regionStart() < matcher.regionEnd()) { foundMatch = false; for (int i = 0; i < patterns.length; ++i) { matcher.usePattern(patterns[i]); if (matcher.lookingAt()) { // do something on token System.out .println("found match for " + patterns[i] + ": " + src.substring(matcher.start(), matcher.end())); foundMatch = true; // next token matcher.region(matcher.end(), matcher.regionEnd()); break; } } if (!foundMatch) // unknown character throw new IllegalStateException("Unknown token at " + matcher.regionStart()); }

class Pattern

The Pattern class represents a regular expression pattern. Follow link to read common regular expression syntax.

method description
static method
compile(String regex) Compiles the given regular expression into a pattern.
compile(String regex,
    int flags)
Compiles the given regular expression into a pattern with the given flags.
matches(String regex,
    CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.
quote(String s) Returns a literal pattern String for the specified String.
instance methods
asPredicate() Creates a predicate which can be used to match a string.
flags() Returns this pattern's match flags.
matcher(CharSequence input) Creates a matcher that will match the given input against this pattern.
pattern() Returns the regular expression from which this pattern was compiled.
split(CharSequence input) Splits the given input sequence around matches of this pattern.
split(CharSequence input,
    int limit)

Splits the given input sequence around matches of this pattern.

The limit parameter specifies the number of times the pattern is applied and therefore affects the length of the resulting array. A non-positive value means that the pattern will be applied as many times as possible and the array can have any length.

splitAsStream(
    CharSequence input)
Creates a stream from the given input sequence around matches of this pattern.
toString() Returns the string representation of this pattern.

class Matcher

method description
static method
quoteReplacement( String s) Returns a literal replacement String for the specified String.
instance methods
appendReplacement( StringBuffer sb, String replacement) Implements a non-terminal append-and-replace step.
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
    m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
// one dog two dogs in the yard
System.out.println(sb.toString());