我正在写一个翻译器,而不是任何严肃的项目,只是为了好玩并且变得对正则表达式更熟悉一些。从下面的代码我想你可以找出我要去哪里(cheezburger任何人?)。确定使用Regex.Matches匹配哪种模式
我使用的字典使用正则表达式列表作为键,字典值是List<string>
,其中包含替换值的更多列表。如果我要这样做,为了弄清楚替补是什么,我显然需要知道关键是什么,我怎样才能找出哪种模式引发了比赛?
var dictionary = new Dictionary<string, List<string>>
{
{"(?!e)ight", new List<string>(){"ite"}},
{"(?!ues)tion", new List<string>(){"shun"}},
{"(?:god|allah|buddah?|diety)", new List<string>(){"ceiling cat"}},
..
}
var regex = "(" + String.Join(")|(", dictionary.Keys.ToArray()) + ")";
foreach (Match metamatch in Regex.Matches(input
, regex
, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture))
{
substitute = GetRandomReplacement(dictionary[ ????? ]);
input = input.Replace(metamatch.Value, substitute);
}
是我试图实现的,还是有更好的方法来实现这种疯狂?
'(?!ues)tion'这是没有意义的,因为这与'tion'一样。也许你想要负面的后顾之忧? '(?<!UE)的tion'?此外,“神”,而不是“虔诚”。 – polygenelubricants 2010-06-24 16:54:41
......和佛,不是佛......什么是天花猫? – 2010-06-24 19:23:32
@Tim:我认为这是对病毒营销活动的一个参考,它让一只猫摆脱了吊扇。 – 2010-06-25 12:44:03