Kethryweryn
Kethryweryn

Reputation: 639

Underscore naming convention regex

What is the regex for variables using underscore naming convention?

For the sake of research, I will remind first that the naming conventions are a very important part of programming. There is a lot of debate about them, even here on SE.

As for my specific use case, I wanted to use an XSD validation for a process described in XML. As this would be imported into a database where some fields would be used as keys, I wanted to restrict the naming to the classic "underscore separated" naming convention.

I was surprised to find out the question has rarely been asked. The only reference I could find on it was a description in a software designed to check coding style written for Java.

The closest thing to what I wanted was

^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$

Upvotes: 2

Views: 1321

Answers (1)

Kethryweryn
Kethryweryn

Reputation: 639

I would suggest using :

[a-z][a-z0-9]*(_[a-z0-9]+)*

Note that the same pattern can be used for CONSTANTS_NAMING_CONVENTION by simply replacing the a-z parts by A-Z.

Upvotes: 2

Related Questions