C# 正则表达式来查找非关键字
我可以使用这样的模式来查找关键字:C# 正则表达式来查找非关键字,c#,regex,match,C#,Regex,Match,我可以使用这样的模式来查找关键字: //find keyword line = @"abc class class_ def this gh static"; string Pattern; MatchCollection M1; Pattern = @"(?<![a-zA-Z0-9_])(class|function|static|this|return)(?![a-zA-Z0-9_])"; M1 = Regex.Matches(line, Pattern); for (int i =
//find keyword
line = @"abc class class_ def this gh static";
string Pattern;
MatchCollection M1;
Pattern = @"(?<![a-zA-Z0-9_])(class|function|static|this|return)(?![a-zA-Z0-9_])";
M1 = Regex.Matches(line, Pattern);
for (int i = 0; i < M1.Count; i++)
output += M1[i].ToString() + '\n';
//查找关键字
行=@“abc类定义此gh静态”;
字符串模式;
匹配集合M1;
Pattern=@”(?我认为正则表达式不是这个用例的好方法
关键字很难维护,模式会随着关键字数量的增加而增长,与Contains()
相比会变慢
将字符串[]
列表
或哈希集
用作关键字
string line = @"abc class class_ def this gh static";
string[] keywords = { "class", "function", "static", "this", "return" };
string outputexclude = string.Join("\n", line.Split().Where(x => !keywords.Contains(x)));
string outputinclude = string.Join("\n", line.Split().Where(keywords.Contains));
我认为正则表达式不是这个用例的好方法
关键字很难维护,模式会随着关键字数量的增加而增长,与Contains()
相比会变慢
将字符串[]
列表
或哈希集
用作关键字
string line = @"abc class class_ def this gh static";
string[] keywords = { "class", "function", "static", "this", "return" };
string outputexclude = string.Join("\n", line.Split().Where(x => !keywords.Contains(x)));
string outputinclude = string.Join("\n", line.Split().Where(keywords.Contains));
您尝试过什么,哪些不起作用?请注意,您的正则表达式基本上是\b(?:class | function | static | this | return)\b
(如果您只需要处理ASCII,请使用RegexOptions.ECMAScript
选项)。您想要实现什么?您尝试过什么,哪些不起作用?请注意,您的正则表达式基本上是\b(?:class | function | static | this | return)\b
(如果您只需要处理ASCII,请使用RegexOptions.ECMAScript
选项)。您想要实现什么?在我的编译器中,我需要使用ToArray()
来成功运行thx,在我的编译器中,我需要使用ToArray()
来成功运行