Pattern Class

Represents a compiled representation of a regular expression.

System

The following are methods for Pattern.

  • compile(regExp)

    Compiles the regular expression into a Pattern object.

  • matcher(stringtoMatch)

    Creates a Matcher object that matches the input string stringtoMatch against this Pattern object.

  • matches(regExp, stringtoMatch)

    Compiles the regular expression regExp and tries to match it against the specified string. This method returns true if the specified string matches the regular expression, false otherwise.

  • pattern()

    Returns the regular expression from which this Pattern object was compiled.

  • quote(yourString)

    Returns a string that can be used to create a pattern that matches the string yourString as if it were a literal pattern.

  • split(regExp)

    Returns a list that contains each substring of the String that matches this pattern.

  • split(regExp, limit)

    Returns a list that contains each substring of the String that is terminated either by the regular expression regExp that matches this pattern, or by the end of the String.

Compiles the regular expression into a Pattern object.

public static Pattern compile(String regExp)

Type: System.Pattern

Creates a Matcher object that matches the input string stringtoMatch against this Pattern object.

public Matcher matcher(String stringtoMatch)

Type: Matcher

Compiles the regular expression regExp and tries to match it against the specified string. This method returns true if the specified string matches the regular expression, false otherwise.

public static Boolean matches(String regExp, String stringtoMatch)

Type: Boolean

If a pattern is to be used multiple times, compiling it once and reusing it is more efficient than invoking this method each time.

Note that the following code example:

produces the same result as this code example:

Returns the regular expression from which this Pattern object was compiled.

public String pattern()

Type: String

Returns a string that can be used to create a pattern that matches the string yourString as if it were a literal pattern.

public static String quote(String yourString)

Type: String

Metacharacters (such as $ or ^) and escape sequences in the input string are treated as literal characters with no special meaning.

Returns a list that contains each substring of the String that matches this pattern.

public String[] split(String regExp)

Type: String[]

In API version 34.0 and earlier, a zero-width regExp value produces an empty list item at the beginning of the method’s output.

The substrings are placed in the list in the order in which they occur in the String. If regExp does not match the pattern, the resulting list has just one element containing the original String.

Returns a list that contains each substring of the String that is terminated either by the regular expression regExp that matches this pattern, or by the end of the String.

public String[] split(String regExp, Integer limit)

  • regExp: Type: String

  • limit: Type: Integer (Optional) Controls the number of times the pattern is applied and therefore affects the length of the list.

    • If limit is greater than zero:

      • The pattern is applied a maximum of (limit – 1) times.
      • The list’s length is no greater than limit.
      • The list’s last entry contains all input beyond the last matched delimiter.
    • If limit is non-positive, the pattern is applied as many times as possible, and the list can have any length.

    • If limit is zero, the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.

Type: String[]

In API version 34.0 and earlier, a zero-width regExp value produces an empty list item at the beginning of the method’s output.