Apologies for the poor formatting; first time poster.
I'm working with an InboundEmail object being sent it from a form from one of our internal systems - it's basically just sending an HTML email into Salesforce. We want to get the answers that have been provided on the form, and RegEx is the way we're trying to do it. Here's a snippit of the HTML coming into Salesforce:
<td><img src="/images/spacer.gif" width="10" height="1"></td>
<td><span style="color:;"><b>Description of Incident</b></span></td>
</tr>
<tr>
<td colspan="3"><img src="/images/spacer.gif" width="1" height="5"></td>
</tr>
<tr>
<td colspan="2"><img src="/images/spacer.gif" width="10" height="1"></td>
<td style="width:100%">The incident failed for no good reason.</td>
</tr>
<tr>
<td colspan="3"><img src="/images/spacer.gif" width="1" height="15"></td>
</tr>
In the above example, the question is Description of incident and the answer is The incident failed for no good reason. So the RegEx pattern we're using is the following:
Desc((.|\n)?)%.
This will get us everything from the word Desc right up to the tag beside the answer. I've tested this in RegEx builders and can confirm this is the case.
So here's the APEX code I'm using in Salesforce, this gets triggered when an email is sent into SF:
String descriptionRegex = 'Desc((.|\n)?)%.';
String extracteddescription = '';
String finaldescription = '';
Pattern descriptionPattern = Pattern.compile(descriptionRegex);
Matcher descriptionMatcher = descriptionPattern.matcher(email.htmlBody);
if (descriptionMatcher.matches()) {
System.debug('Found a match'); extractedDescription = descriptionMatcher.group(0); System.debug('Group extract: ' + extracteddescription); System.debug('index of percentage: ' + extracteddescription.indexOf('%')); System.debug('index of less than: ' + wxtracteddescription.lastIndexOf('<'));
finaldescription = extracteddescription.substring((extracteddescription.indexOf('%') + 3), extracteddescription.lastIndexOf('<')); System.debug('Final description: ' + finaldescription); } else { System.debug('Did not find a match'); }
Expected result
With the above code, I'm getting the block of text (previously confirmed that I should be returning using the above Pattern), if I've found a match, then extract the answer using a String.substring method.
Actual result
When the code runs, I hit the else condition, and no pattern is matched.
Any idea why my code isn't working?
Also some supplementary information; the htmlBody variable is returning the HTML of the email. I've put said variable result into a debug statement, and ran the output HTML against my RegEx and can confirm it should be picked up
stripHtmlTags(email.htmlBody)
and assigning it to a new string variable before running your pattern matcher to see if that doesn't solve your problems.