正则表达式替换

编程入门 行业动态 更新时间:2024-10-24 02:31:59
本文介绍了正则表达式替换 - 多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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

更多推荐

正则表达式替换

本文发布于:2023-10-23 13:48:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!