I'm especially terrible with Regex, but reworked one I found online to suit my needs, but it keeps throwing the "no viable alternative at character '\'" error. This pattern works when tested in regex101.com, regexplanet.com, and rubular.com. I thought I had escaped everything that needed escaping. Right, now I'm out of my depth with this. Hopefully someone here can give me some insight.
In case anyone needs to know, the regex needs to find the first email address in an the email body (which is from a submitted form) that immediately follows the word "From:" (at this point I'm unsure if there will be a space after "From:" or not), since there could be multiple email addresses in the text.
Thanks in advance to anyone who can help!
Here's my code:
if (email.Subject.Contains('xxxx') || email.fromAddress == 'xxxx'){
Pattern myPattern = Pattern.compile('From:(\\s(?:[a-z0-9!#$%&'*+\\/=?^_'{|}~-]+(?:\\.[a-z0-9!#$%&'*+\\/=?^_'{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\]))');
Matcher myMatcher = myPattern.matcher(email.plainTextBody);
Set<String> emails = new Set<String>();
while(myMatcher.find()) {
emails.add(matcher.group(1));
}
}