当前位置:网站首页>C# BBcode 转 Markdown
C# BBcode 转 Markdown
2022-08-04 14:49:00 【林德熙】
本文告诉大家一个简单的方法从 BBcode 转为 Markdown
本文的方法都是使用正则转换,现在支持的代码只有很少的常用标签,如果大家发现有转换失败的,请帮我修改代码,估计代码我不会进行修改。
最重要的就是转换 url 和 image,那么主要就告诉大家如何转换这两个
private static string ConvertUrl(string str)
{
var regex = new Regex(@"\[url=(.+?)\]((?:.|\n)+?)\[\/url\]");
return regex.Replace(str, "[$2]($1)");
}这就是转换 url 的代码,里面用了正则。一般使用正则拿到的数据就是(里的数据,如上面的代码,可能看起来有些复杂,那么用下面的代码告诉大家。
例如 需要拿出 123lindexifoo中的 lindexi ,那么正则可以这样写
123(lindexi)foo如果需要拿到 lindexi ,可以使用下面代码
var regex = new Regex(@"123(lindexi)foo");
if (regex.Match(str).Groups[1].Value == "lindexi")
{
}实际使用会在 regex.Match 之后判断是否成功,而不是直接拿出来。从上面代码可以看到我使用了1而不是0,那么下面的代码拿到的字符串?
var regex = new Regex(@"123(lindexi)foo");
str = regex.Match(str).Groups[0].Value;这个 str 拿到是 123lindexifoo 就是原来匹配到的所有字符串。
如果需要替换某个字符串,可以使用下面的代码
str = "123lindexifoo";
var regex = new Regex(@"123(lindexi)foo");
str = regex.Replace(str, "csdn");
str == "csdn"如果想把上面的字符串替换为 lindexi csdn ,那么可以使用下面代码
str = "123lindexifoo";
var regex = new Regex(@"123(lindexi)foo");
str = regex.Replace(str, "$1csdn");可以看到上面的替换使用了 1 ,这个就是第一个匹配拿到的字符串。如果需要拿第二个,就是使用2,所以做这个很简单
从上面的代码可以看到,转换 image 可以使用代码
var regex = new Regex(@"\[img\]((?:.|\n)+?)\[\/img\]");
return regex.Replace(str, "");转换其他的代码上传到 github ,如果是在我博客可以直接看到
边栏推荐
猜你喜欢

Theory 1: Deep Learning - Detailed Explanation of the LetNet Model

ASA归因:如何评估关键词的投放价值

JCMsuite Application: Oblique Plane Wave Propagation Transmission Through Aperture

Compound Refractive Lenses for X-ray Focusing

Find My技术|防止你的宠物跑丢,苹果Find My技术可以帮到你

leetcode:251. 展开二维向量

用了TCP协议,就一定不会丢包吗?

【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常

【Web技术】1401- 图解 Canvas 入门

How to automatically renew the token after it expires?
随机推荐
B.构造一个简单的数列(贪心)
[The Art of Hardware Architecture] Study Notes (1) The World of Metastability
leetcode: 251. Expanding 2D Vectors
leetcode:255 验证前序遍历序列二叉搜索树
什么,你告诉我?作用域也分种类?
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
【Web技术】1401- 图解 Canvas 入门
1401 - Web technology 】 【 introduction to graphical Canvas
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
关于pnpm包管理器的版本问题
xampp安装包含的组件有(php,perl,apche,mysql)
1403. 非递增顺序的最小子序列
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
FRED应用:毛细管电泳系统
Technology sharing | Mini program realizes audio and video calls
Leetcode: 215 disorderly to find the first big k element in the array
输入输出流总结
Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
OAID是什么
G.登山小分队(暴力&dfs)