gpt4 book ai didi

C# 正则表达式错误未终止 [] 集

转载 作者:行者123 更新时间:2023-11-30 14:15:26 28 4
gpt4 key购买 nike

我收到此错误(我正在使用 C#):

parsing "(--[[.?]])|(--[[.)" - Unterminated [] set.

尝试添加Lua的多注释代码时。它应该是这样的:

--[[
Hello
]]

但是,当我取出这个 Regex 末尾的 ']]' 时:

(--[[.*?]])|(--[[.*)

它会给我这个错误。但是,如果我在上面添加 ']]',例如:

(--[[.*?]])|(--[[.*]])

它工作得很好。

完整代码如下:

Regex CustomCommentRegex1, CustomCommentRegex2, CustomCommentRegex3;
CustomCommentRegex1 = new Regex(@"--.*$", RegexOptions.Multiline | RegexCompiledOption);
CustomCommentRegex2 = new Regex(@"(--[[.*?]])|(--[[.*)", RegexOptions.Singleline | RegexCompiledOption);
CustomCommentRegex3 = new Regex(@"(--[[.*?]])|(.*]])", RegexOptions.Singleline | RegexOptions.RightToLeft | RegexCompiledOption);

“CustomCommentRegex2”是我收到此“Unterminated [] set”错误的地方。如果我在“CustomCommentRegex2”末尾添加“]]”,我会遇到问题。它将突出显示“--[[ ]]”上方的文本,评论及其内部;无论如何,问题是这个“Unterminated [] set”错误。

最佳答案

来自Regex网站

Because we want to do more than simply search for literal pieces of text, we need to reserve certain characters for special use. In the regex flavors discussed in this tutorial, there are 11 characters with special meanings: the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ). These special characters are often called "metacharacters".

用\

转义括号
(--\[\[.*?]])|(--\[\[.*)

关于C# 正则表达式错误未终止 [] 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10080770/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com