Salesforce Apex includes numerous built-in text handling classes, and in addition to them, the Pattern and Matcher classes are good for performing complex string operations. These classes enable us to define and work with regular expressions, which are of particular importance for data validation, searching, and modification.
In this technical blog, we will find out what the Pattern and Matcher classes are, as well as their methods and purposes in Apex Salesforce and see a few related examples.
What is the Pattern Class?
Salesforce Apex Pattern class offers a way for users to create regular expressions. A regular expression is a type of character sequence which fulfils a specific requirement in terms of the order of characters and can be used to match a string or some substring.
Key Methods in the Pattern Class:
- Pattern.compile(String regex): Compiles a regular expression into a Pattern object. This compiled pattern can be used to create Matcher objects for various strings.
- Pattern.matches(String regex, String input): Checks if a given input string matches the provided regular expression pattern. This is useful for simple one-time checks.
What is the Matcher Class?
The Matcher class works with the Pattern class and provides functionality for finding, extracting, and manipulating text data based on a defined pattern. Matcher allows you to scan through the string and look for places that occur in the specified pattern.
Key Methods in the Matcher Class:
- Matcher.find(): Searches for occurrences of the pattern in the input string. Returns true if at least one match is found.
- Matcher.group(): Returns the last matched part of the string. This is typically used after a find() to get the matched substring.
- Matcher.start(): Returns the starting index of the match within the input string.
- Matcher.end(): Returns the ending index (exclusive) of the match within the input string.
- Matcher.matches(): Checks if the entire input string matches the pattern.
Using Pattern and Matcher in Apex: Examples
Let’s take some examples to understand how the Pattern and Matcher classes work in Apex.
Example 1: Basic Pattern Matching with Pattern and Matcher
Suppose we want to validate email addresses in a user input.
Explanation:
- We use a regular expression (^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$) to validate an email pattern.
- Pattern.compile() creates the pattern, and emailMatcher.matches() checks if the email matches the pattern.
Example 2: Extracting Information Using Matcher
Suppose you want to extract all numbers from a given string.
Explanation:
- \d+ is a regular expression to match any sequence of digits.
- numberMatcher.find() searches for occurrences of the pattern in the input string.
- numberMatcher.group() returns the last matched substring, which is each number in this case.
Example 3: Replacing Text Using Pattern and Matcher
Let’s say we want to mask part of a credit card number.
Explanation:
- \d{4}-\d{4}-\d{4}-(\d{4}) matches the last 4 digits of a credit card number.
- matcher.replaceAll(‘****-****-****-$1’) replaces all but the last four digits with ****, using $1 to refer to the matched last group.
Common Use Cases of Pattern and Matcher Classes in Apex
The Pattern and Matcher classes in Apex can be applied to many real-world use cases:
- Email and phone number validation to ensure proper format.
- Data extraction such as pulling out all dates, numbers, or specific keywords from large text bodies.
- Text transformations like masking confidential information, formatting text, and more.
- Advanced search within strings to look for patterns and apply conditional logic based on matches.
Performance Tips
- Use Patterns Sparingly: Regular expressions can be CPU-intensive. Avoid using them in loops that process large data sets unless necessary.
- Optimize Patterns: Simple patterns are faster. For example, \d{4} is faster than (\d)(\d)(\d)(\d).
- Consider Bulk Processing: If working in bulk, consider optimizing your Apex code with patterns only when necessary to avoid hitting governor limits.
Conclusion
The Pattern and Matcher lessons in Salesforce Apex offer effective gear for textual content validation, extraction, and manipulation using everyday expressions.
By mastering those instructions, builders can write more bendy and robust code, mainly for tasks that involve information validation, extraction, or transformation.
Regular expressions can appear complicated at first, however, with exercise and careful use, they offer a concise way to address complicated text processing tasks correctly.