I have requirement to prevent a street address field in Salesforce from accepting any value that contains P. O. Box Post Office Box P o Box

I thought the rule below would work fut for some reason the the check syntax button in Salesforce kicks out this error. "Error: Syntax error".

Any help will be appreciated.

NOT(REGEX(Sold_To_Street__c, "?i)\b(?:p\.?\s*o\.?|post\s+office)\s+box\b"))

Your regular expression is not syntactically correct. You are missing the leading parentheses before ?1. Expression should be


This is good online site to test regex.

In your formula you will need to escape the backslash:

  REGEX( (Sold_To_Street__c, "(?i)\\b(?:p\\.?\\s*o\\.?|post\\s+office)\\s+box.*")

REGEX() cannot be used in forumla fields our custom buttons and links. I'm assuming this formula is on a validation rule.

Also, it looks like REGEX only returns true if the entire string matches the regular expression. Modified the expression with .* to include in characters that follow.

See Salesforce Documenation on REGEX()

  • Thanks a lot that makes sense. @developer wonk. Unfortunately I am still returning the same error NOT(REGEX(Sold_To_Street__c, "(?i)\b(?:p\.?\s*o\.?|post\s+office)\s+box\b") – NewCoder Sep 22 '15 at 17:34
  • Backslashes need to be escaped when using in a forumula – Developer Wonk Sep 22 '15 at 18:29
  • Yes. I am using it in a validation rule. When the backslashes are escaped the function no longer catches any expression. I have played all sorts of permutation of the parentheses. This expression does not work. It validates in the Salesforce syntax checker but does not catch the P. O. Box entry. REGEX( Sold_To_Street__c, "(?i)\\b(?:p\\.?\\s*o\\.?|post\\s+office)\\s+box\\b") – NewCoder Sep 22 '15 at 19:59
  • @NewCode it looks like the regular expression in REGEX() has to match the entire string. – Developer Wonk Sep 22 '15 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.