Skip to main content
3 of 3
deleted 18 characters in body

Apex Pattern class behaves like Java Pattern class. According to Java documentation you have to escape backslash if it is present in regex expression

The backslash character ('') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.


Just escape \ in your regex with additional backslashes. You want to have regex (\\w+\\.\\w+)$


Account aa = [SELECT id, Website from Account WHERE Id = '0016F00002QeRqw'];
String d = aa.Website;
String regexx = '(\\w+\\.\\w+)$';
Pattern mPat = Pattern.compile(regexx);
Matcher myMatch = mPat.matcher(d);
myMatch.find();
System.debug('Domain:' + myMatch.group());