Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式来查找非关键字_C#_Regex_Match - Fatal编程技术网

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()
来成功运行