+ Start a Discussion
Jeff TalbotJeff Talbot 

Where is the documentation for REGEX?

Where can I find the documentation on building the "regex_text" part of the REGEX function? All I can find in Help/Training/Release Notes is  a few examples of a working formula. There is nothing that explains the different characters and codes that can be used in the "regex_text" part of this function.
 
Syntax:
REGEX(text, regex_text)
 
Instructions:
Replace text with the text field, and regex_text with the regular expression you want to match
 
Examples:
REGEX(Drivers_License__c, "([A-Z]\\d{7})?")
REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?")
REGEX(Credit_Card_Number__c, "(((\\d{4}-){3}\\d{4})|\\d{16})?")
REGEX(IP_Address__c, "^((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]?)$" )
REGEX(Social_Security_Number__c, "[0-9]{3}-[0-9]{2}-[0-9]{4}")
Best Answer chosen by Admin (Salesforce Developers) 
EricBEricB
The REGEX function uses the Java Platform SE 6 syntax, so you can look at the Java reference materials to learn the syntax.  There are also many REGEX tutorials and useful examples around the web.  Here are a couple sites I found:

http://www.regular-expressions.info/
http://regexlib.com/

Also, O'Reilly has a great book called "Mastering Regular Expressions".

When using external examples with our REGEX formula function, the key thing to keep in mind is that the backslash characters (\) must be changed to double backslashes (\\) because backslash is an escape character in Salesforce.

Anyone else have any favorite sources for regex wisdom?

Cheers,
Eric Bezar
force.com Platform Product Management

All Answers

rockchick322004rockchick322004
All formula functions are documented in the online help and training.  Click on Help & Training link in the app and search for REGEX.  The first hit should be the function documentation.  We did not document the regular expression syntax itself because we are just leveraging the Java syntax, which is already documented.  In the Tips section of the function doc we have a link to the Java documentation...

"Regular expression syntax is based on Java Platform SE 6 syntax. However, backslash characters (\) must be changed to double backslashes (\\) because backslash is an escape character in Salesforce."

Hope this helps!  Regular expressions are very powerful and will make your formulas shorter and more efficient!
EricBEricB
The REGEX function uses the Java Platform SE 6 syntax, so you can look at the Java reference materials to learn the syntax.  There are also many REGEX tutorials and useful examples around the web.  Here are a couple sites I found:

http://www.regular-expressions.info/
http://regexlib.com/

Also, O'Reilly has a great book called "Mastering Regular Expressions".

When using external examples with our REGEX formula function, the key thing to keep in mind is that the backslash characters (\) must be changed to double backslashes (\\) because backslash is an escape character in Salesforce.

Anyone else have any favorite sources for regex wisdom?

Cheers,
Eric Bezar
force.com Platform Product Management

This was selected as the best answer
Jeff TalbotJeff Talbot
Thank you EricB and mscotton. I guess I need to go learn Java now. Seems a little intimidating for a beginner, but I've done just fine learning formula syntax in SF, so hopefully I'll catch on with Java. Thanks for the links.
EricBEricB
No, you don't have to learn Java, but you do have to learn regular expression syntax, which is fairly tricky for non-programmers.  The reason for the Java link is that there are numerous variations of regular expression syntax in different programming languages -- Perl and Java, to name a couple.  We have implemented the Java version of regular expression syntax.

Definitely look at the regex examples in the salesforce online help, too.  I've found that the best way to learn is to study examples.

And I will bet dollars to donuts that if you post a specific regex question on this board, someone in the community will help you out.

Good luck,
Eric


AcMEGXAcMEGX
Hi Eric Bezar,

    YOU ARE THE MAN! ("When using external examples with our REGEX formula function, the key thing to keep in mind is that the backslash characters (\) must be changed to double backslashes (\\) because backslash is an escape character in Salesforce.")
You just save me hours of work! Good Job! Thanks!

AcMEGX
Jeff BloomerJeff Bloomer

I seem to be having a real bear of a time getting this to work for a Flow validation on a text field.  All I want to do is have it evaluate a text field in a flow screen to ensure that it doesn't contain special characters that would cause a MS Filename to puke.

 

Any ideas?

Philip DeGraafPhilip DeGraaf
The example given in the PDF doesn't give all caps for the Canada postal code. I've spent several hours trying to find info on Regular Express and what I've found isn't working in Salesforce [A-Z] . I copied and pasted right into Salesforce as is and it's not case sensitive. I'm doing this in Leads, if that makes any difference?

AND(
OR(Country = "CAN", Country = "CA", Country = "Canada"),
NOT(REGEX(PostalCode,
"((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"))



Here is the PDF link
http://resources.docs.salesforce.com/200/6/en-us/sfdc/pdf/salesforce_useful_validation_formulas.pdf