4

我有一个正则表达式来检查脚本语言中的有效标识符。它们以字母或下划线开头,后面可以跟 0 个或多个字母、下划线、数字和 $ 符号。但是,如果我打电话

Util.IsValidIdentifier( "hello\n" );

它返回真。我的正则表达式是

const string IDENTIFIER_REGEX = @"^[A-Za-z_][A-Za-z0-9_\$]*$";

那么“\n”是如何通过的呢?

4

3 回答 3

5

The $ matches the end of lines. You need to use \z to match the end of the text, along with RegexOptions.Multiline. You might also want to use \A instead of ^ to match the beginning of the text, not of the line.

Also, you don't need to escape the $ in the character class.

于 2013-06-27T16:13:46.390 回答
1

Because $ is a valid metacharacter which means the end of the string (or the end of the line, just before the newline). From msdn:

$: The match must occur at the end of the string or before \n at the end of the line or string.

You should escape it: \$ (and add \z if you want to match the end of the string there).

于 2013-06-27T16:14:43.483 回答
0

你的结果是true因为hello\n你不需要$在字符类内部转义,因此反斜杠是匹配的,因为你在字符类中有一个反斜杠(视为文字)。

尝试这个:

const string IDENTIFIER_REGEX = @"^[A-Za-z_][A-Za-z0-9_$]*$";

由于您正在测试一行中的变量名称,因此您可以将$其用作字符串的结尾。

于 2013-06-27T17:44:20.397 回答