C# - 你能做一个正则表达式搜索模式数组吗?



我是一名网络工程师,使用多个版本的思科IOS,需要使用多个正则表达式模式。 有没有办法构建正则表达式模式数组?

我已经在各种其他程序中使用了正则表达式模式,但是当我开始构建它时,到目前为止我有 3 种模式格式需要搜索,毫无疑问,当我通过我们的网络时会添加更多。

我已经尝试了通常的:

Regex someVariable[] = new Regex();

这是我目前所拥有的,并希望将其清理到一个数组中,以便将来通过模式进行循环搜索,或者我可以根据需要单独调用它们 someVariable[0] 等。

Regex intRegex = new Regex(@"((?<!S)[A-Za-z]{2}d{1,5}S+s+)|(s[A-Za-z]{7,15}d{1,5}S+s+)", RegexOptions.Compiled);
Regex psRegex = new Regex(@", ids+d{10},", RegexOptions.Compiled);
Regex cidRegex = new Regex(@"d{3}-d{3}-d{4}", RegexOptions.Compiled);
Regex vcidRegex = new Regex(@"vcidsd{10},", RegexOptions.Compiled);

我不确定我使用 c#的选择是什么,因为我以前从未使用 c# 走过这条路,过去以前只在整个程序中使用 1 或 2 个正则表达式搜索模式。

试试这个

Regex[] regexCollection = new Regex[4];
Regex intRegex = new Regex(@"((?<!S)[A-Za-z]{2}d{1,5}S+s+)|(s[A-Za-z]{7,15}d{1,5}S+s+)", RegexOptions.Compiled);
Regex psRegex = new Regex(@", ids+d{10},", RegexOptions.Compiled);
Regex cidRegex = new Regex(@"d{3}-d{3}-d{4}", RegexOptions.Compiled);
Regex vcidRegex = new Regex(@"vcidsd{10},", RegexOptions.Compiled);
regexCollection[0] = intRegex;
regexCollection[1] = psRegex;
regexCollection[2] = intRegex;
regexCollection[3] = vcidRegex;