Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know why the Matcher in Salesforce does not find text contained within <> (the "less than" and "greater than" signs)? The sample code below, adapted from "Using Patterns and Matchers", fails the final assertion in API 29.0.

// First, instantiate a new Pattern object "MyPattern"
Pattern MyPattern = Pattern.compile('a*b');

// Then instantiate a new Matcher object "MyMatcher"
Matcher MyMatcher = MyPattern.matcher('<aaaaab>');

// You can use the system static method assert to verify the match,
// but this assertion fails because the expected match is not found
share|improve this question
up vote 4 down vote accepted

Doesn't matches() do a whole-string determination of whether it fits the regex? Like you might use for validating input strings? Might have more luck with the find() method:

Pattern MyPattern = Pattern.compile('a*b');
Matcher MyMatcher = MyPattern.matcher('<aaaaab>');
while(MyMatcher.find()) {
    System.debug(; //"aaaaab"

Eyeballing the question for your intention, are you looking for something like this?

Pattern MyPattern = Pattern.compile('<code>([^<]+)</code>');
Matcher MyMatcher = MyPattern.matcher('<code>herp</code><br /><code>derp</code>');
while(MyMatcher.find()) {
    System.debug(; //"herp" and "derp"

You're probably already aware of the usual caveats :-)

share|improve this answer
Thanks again for the assistance! I was wondering where you found mention of the Matcher.find() method, until I re-read the "Using Match Operations" page. It seems like this important method could use some more publicity in the documentation:… – Marty C. Feb 15 '14 at 15:17
np @MartyC. - I've added an example – bigassforce Feb 15 '14 at 15:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.