Take the 2-minute tour ×
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.

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
System.assert(MyMatcher.matches());
share|improve this question

1 Answer 1

up vote 2 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(MyMatcher.group(0)); //"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(MyMatcher.group(1)); //"herp" and "derp"
}

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

share|improve this answer
1  
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: salesforce.com/us/developer/docs/apexcode/… –  Marty C. Feb 15 at 15:17
    
np @MartyC. - I've added an example –  bigassforce Feb 15 at 15:22

Your Answer

 
discard

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.