How to define a C ++ identifier string?

eg:

isValidCppIdentifier("_foo") // returns true
isValidCppIdentifier("9bar") // returns false
isValidCppIdentifier("var'") // returns false

I wrote some quick code, but it fails: my regular expression "[a-zA-Z_$][a-zA-Z0-9_$]*" and I just do regex.IsMatch(inputString).

Thank..

+3
source share
2 answers

It should work with some added bindings:

"^[a-zA-Z_][a-zA-Z0-9_]*$"

If you really need to support ridiculous identifiers using Unicode, feel free to read one of the different versions of the standard and add all ranges to your regular expression (for example, pages 713 and 714 http://www-d0.fnal.gov/~dladams/cxx_standard .pdf )

+2
source

Matti ​​++-, ++. , , L"wchar_t string", L . Unicode.

Clang, Apple, , . , clang_createTranslationUnitFromSourceFile clang_tokenize.

, \Uxxxx - . - . LLVM , ... .

, GCC , , , cpp_lex_direct.

+1

Source: https://habr.com/ru/post/1762006/


All Articles