本文介绍了正则表达式替换 - 多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有20个左右的字,我需要更换与文本块各种各样的字符。有没有办法做到这一点在一个单一的正则表达式,以及如何将这种正则表达式是什么?还是有更简单的方式来做到这一点。NET?
I have 20 or so characters that I need to replace with various other characters in a block of text. Is there a way to do this in a single regex, and what would this regex be? Or is there an easier way to do this in .NET?
例如,从我的映射表中摘录
For example, an excerpt from my mapping table is
OE => OE
Z =>ž
Y =>ÿ
A =>一个
A =>一个
A =>一个
A =>一个
△=>自动曝光
œ => oe
ž => z
Ÿ => Y
À => A
Á => A
 => A
à => A
Ä => AE
推荐答案
如果你真的很喜欢做单正则表达式,有办法做到这一点。
If you really like to do it in single regex, there is way to do that.
Dictionary<string, string> map = new Dictionary<string, string>() {
{"œ","oe"},
{"ž", "z"},
{"Ÿ","Y"},
{"À","A"},
{"Á","A"},
{"Â","A"},
{"Ã","A"},
{"Ä","AE"},
};
string str = "AAAœžŸÀÂÃÄZZZ";
Regex r = new Regex(@"[œžŸÀÂÃÄ]");
string output = r.Replace(str, (Match m) => map[m.Value]);
Console.WriteLine(output);
结果
AAAoezYAAAAEZZZ
这篇关于正则表达式替换 - 多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!