In Apex we can access a field using the dot notation

Example : Owner.Name

I am looking for a regex to validate that notation

I come up we the regex below

String validateDotNotation='([a-zA-Z]([a-zA-Z1-9]|[a-zA-Z1-9]_[a-zA-Z1-9])*[a-zA-Z1-9](__[rRcC])?\\.)*([a-zA-Z]([a-zA-Z1-9]|[a-zA-Z1-9]_[a-zA-Z1-9])*[a-zA-Z1-9](__[cC])?)'

Some explanations

[a-zA-Z] : validate the first char must be alphabetic upper or lower case
[a-zA-Z1-9] : validate char must be alphanumeric 
[a-zA-Z1-9]_[a-zA-Z1-9] : validate _ must not be two consecutive _
[a-zA-Z1-9]|[a-zA-Z1-9]_[a-zA-Z1-9] : validate middle field name chars can be alphanumeric & non consecutive _
[a-zA-Z]([a-zA-Z1-9]|[a-zA-Z1-9]_[a-zA-Z1-9])*[a-zA-Z1-9] : validate a field name
(__[rRcC])? : custom or relationship field ending 
(__[cC])? : custom field ending

Salesforce naming rules for a field API Name

Naming rules

Can you tell me if it's correct or if there is a better way to do it ?

  • Is this the field label or the API name? Mar 16, 2017 at 10:35
  • @MartinLezer API Name
    – benahm
    Mar 16, 2017 at 10:41
  • 2
    What about custom __r / __c?
    – kurunve
    Mar 16, 2017 at 10:43
  • @kurunve thanks just thought about that I'll update the regex
    – benahm
    Mar 16, 2017 at 10:43
  • Personally I would do that as a trigger, so I can confirm that field/path exists and valid by dynamic query, but that may be not that lightweight (Sorry, thought that validation rule is being used)
    – kurunve
    Mar 16, 2017 at 10:45

2 Answers 2


"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

-Jamie Zawinski

Regular expressions do have their uses, but I find myself trying to avoid using regex if I can.

I think there's a better solution in your case.

Using Apex, you can pull the fields from an object (any sObject).

Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();

Using this approach, you can do something that your regex will never be able to, you can verify that the given field name is actually a field on your target object. Given the line of code above, and assuming you have a variable called fieldName...

    // add an error to be displayed on your Visualforce page here

It gets a bit more involved if you want to do this for related fields, but using String.split('.'), Schema.getGlobalDescribe(), and Schema.DescribeFieldResult.getReferenceTo(), would accomplish what you're looking to do here.


Field validation


Reference validation



[a-zA-Z] : Validate the first char must be alphabetic upper or lower case
[a-zA-Z0-9]* : Following the first char can be any amount of alphanumeric characters
(_[a-zA-Z0-9]+)* : If we have an underscore must be followed by at least one alphanumeric character
(__[rRcC])? : custom or relationship field ending optional (? = matches none or one time)
(__[cC])? : custom field ending (? = matches none or one time)

The issue with your expression

String validateDotNotation='([a-zA-Z]([a-zA-Z1-9]|[a-zA-Z1-9]_[a-zA-Z1-9])*[a-zA-Z1-9](__[rRcC])?\\.)*([a-zA-Z]([a-zA-Z1-9]|[a-zA-Z1-9]_[a-zA-Z1-9])*[a-zA-Z1-9](__[cC])?)'

Is that you can't have only one char before the first char, like a_b__c, or a__c. And can't have only a char at the end following an underscore like hello_f__c And that you are leaving 0 out with 1-9

  • 5
    Please add some explanation if these expressions actually answer the question.
    – Adrian Larson
    Oct 2, 2017 at 23:35

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.