KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method: Validate 4D Identifier Name
PRODUCT: 4D | VERSION: 13.1 | PLATFORM: Mac & Win
Published On: July 13, 2012

4D identifiers follow these rules for naming:

  • A name must begin with an alphabetic character or underscore.
  • Thereafter, the name can include alphabetic characters, numeric characters, the space character, and the underscore character.
  • Periods, slashes, quotation marks and colons are not allowed.
  • Characters reserved for use as operators, such as * and +, are not allowed.
  • 4D ignores any trailing spaces.
  • See https://doc.4d.com/4Dv13/help/Title/en/page701.html

Though these are the rules for 4D identifier names, 4D has traditionally been lax in enforcing them. It's certianly possible to find method names created in olders versions with *'s in them, for example. As 4D moves forward, these inconsistencies are slowly being eliminated.

With 4D v13 it is more important than ever to have valid identifier names. The Design Object Access theme of commands, for example, rely on valid identifier names for their functionality. If you want to export a method to disk using the method name, it must be a valid identifier since the file system won't support special characters.

This method can be used to test to see if a string satisfies these rules:

C_TEXT($1;$identifier_t)

C_BOOLEAN($0;$isIdentifier_f)

C_TEXT($pattern_t)

$identifier_t:=$1

// Note:
// \w is shorthand for alphanumeric or underscore, e.g. [a-zA-Z0-9_]
// \s is shorthand for whitespace, e.g. spaces, tabs, etc.

$pattern_t:="^\\w[\\w ]*(?<!\\s)$"


// A little explanation about this pattern:
//
// ^ - matches the beginning of the value; this is a "zero-width" match
// \w - matches a single alpha, number, or underscore character; this is the
//      minimum value for a 4D indentifier
// [\w ]* - matches 0 or more alpha, number, underscore, or space characters
// (?<!\s)$ - matches the end of the value, but then looks back to make sure the
//      end of line $ wasn't preceded by any whitespace. In other words it rejects
//      the value if it has trailing whitespace.
//
// Note that there is a flaw here. 4D does not allow table names to
// start with numbers, but this pattern will allow it. However since
// 4D doesn't allow you to create this case, I don't think it's
// important to validate it.

$isIdentifier_f:=Match regex($pattern_t;$identifier_t)

$0:=$isIdentifier_f

Commented by Josh Fletcher on August 15, 2012 at 2:20 PM
The previous version of this Tech Tip used the pattern "\w[\w ]+\w". This was flawed because it created a 3 character minimum for object names. 4D has no such minimum. The updated pattern allows for 1 character or more, as does 4D.