---
schema_version: "1"
id: 348d48932c2a665ad3c59b176608aae5b8cb4c20853314245c5352edba71fb14
title: Pattern Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_classes_pattern_and_matcher_pattern_methods.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: fcc9e26f298fe96afb1ded708e73cc90cffe71b266c02bbdf9b84578ae062ad7
description: Represents a compiled representation of a regular expression.Compiles the regular expression into a Pattern object. Creates a Matcher object that matches the input string stringtoMatch against this Pattern object. 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. Returns the regular expression from which this Pattern object was compiled. Returns a string that can be used to create a pattern that matches the string yourString as if it were a literal pattern. Returns a list that contains each substring of the String that matches this pattern. 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.
---
# Pattern Class

Represents a compiled representation of a regular expression.

## Namespace

[System](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_System.htm "The System namespace provides classes and methods for core Apex functionality.")

## Pattern Methods

The following are methods for Pattern.

- **[compile(regExp)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_System_Pattern_compile)**  
    Compiles the regular expression into a Pattern object.
- **[matcher(stringtoMatch)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_System_Pattern_matcher)**  
    Creates a Matcher object that matches the input string stringtoMatch against this Pattern object.
- **[matches(regExp, stringtoMatch)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_System_Pattern_matches)**  
    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()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_System_Pattern_pattern)**  
    Returns the regular expression from which this Pattern object was compiled.
- **[quote(yourString)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_System_Pattern_quote)**  
    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)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_System_Pattern_split)**  
    Returns a list that contains each substring of the String that matches this pattern.
- **[split(regExp, limit)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_System_Pattern_split_2)**  
    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.

### compile(regExp)

Compiles the regular expression into a Pattern object.

#### Signature

public static Pattern compile(String regExp)

#### Parameters

- **regExp**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [System.Pattern](#apex_classes_pattern_and_matcher_pattern_methods "Represents a compiled representation of a regular expression.")

### matcher(stringtoMatch)

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

#### Signature

public Matcher matcher(String stringtoMatch)

#### Parameters

- **stringtoMatch**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [Matcher](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_matcher_methods.htm#apex_classes_pattern_and_matcher_matcher_methods "Matchers use Patterns to perform match operations on a character string.")

### 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.

#### Signature

public static Boolean matches(String regExp, String stringtoMatch)

#### Parameters

- **regExp**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **stringtoMatch**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [Boolean](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_boolean.htm#apex_methods_system_boolean "Contains methods for the Boolean primitive data type.")

#### Usage

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

#### Example

Note that the following code example:

Pattern.matches(regExp, input);

produces the same result as this code example:

Pattern.compile(regex).
matcher(input).matches();

### pattern()

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

#### Signature

public String pattern()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

### 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.

#### Signature

public static String quote(String yourString)

#### Parameters

- **yourString**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Usage

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

### split(regExp)

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

#### Signature

public String\[\] split(String regExp)

#### Parameters

- **regExp**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")\[\]

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

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.

#### Usage

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.

### 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.

#### Signature

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

#### Parameters

- **regExp**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **limit**: Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.") (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.

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")\[\]

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/images/icon_note.png)

#### Note

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.