Monday, February 14, 2011

Special escape sequence in Salesforce for Regex Validations

I found this out last week but simply wasn't able to share it. But better share than never! Well, it is somewhat tricky, though clearly specified in the Regex documentation page at developer.salesforce.com.

In a scenario when you want to speciy a regular expression that has a slash ie the '\' symbol, for example, a commonly used regex is '\w', the Salesforce validation interface doesn't allow a single slash, rather it accepts double slash in order to accept the '\w' that we normally would have written, the Salesforce platform accepts this as a '\\w' instead. Perhaps this is a special case wherein the slash symbol itself has to be represented as an escape sequence, and that is why we use double slash.

The gist of the story, whenever you wanna use a Regular expression with a slash, make sure you use double slash (ie '\\w') and that is when Salesforce will understand the regex what you wanted to set.

Happy coding!


No comments:

Post a Comment