c# - 文本、标签、base64 和 RegEx C#
问题描述
晚上好,我有一个正则表达式问题。
我读了一个json,文本的结构如下:
text[TAG1 attributes]othertext[/TAG1][TAG2 attributes]base64string[/TAG2]
我需要将最终文本提取为
text othertext base64string(decode)
输入文本可以是多行。
我该怎么做才能得到这个?
谢谢!
解决方案
这是一个应该与您的情况相匹配的正则表达式:
(?>^|\n|\[[^\]\n]*\]\n*)(?'content'[^\[]+)
您基本上匹配[
ornewline
和之间的任何内容,直到]
和开始在名为content的组中捕获内容,直到下一次出现。[
它C#
看起来像这样:
var input = @"text[TAG1 attributes]othertext[/TAG1][TAG2 attributes]base64string[/TAG2]
text2[TAG1 attributes]othertext2[/TAG1][TAG2 attributes]base64string2[/TAG2]
text3[TAG1 attributes]othertext3[/TAG1][TAG2 attributes]base64string3[/TAG2]";
var expression = new Regex(@"(?>^|\n|\[[^\]\n]*\]\n*)(?'content'[^\[]+)");
Match match = expression.Match(input);
while (match.Success)
{
var content = match.Groups["content"].Value;
Console.WriteLine(content);
match = match.NextMatch();
}
阅读有关Match.Groups的 MS 文档。
推荐阅读
- animation - 动画未在 a-frame 0.9.2 的事件上重新启动
- python - mac地址的字节数组格式
- ruby-on-rails - 关于 Ruby on Rails 最佳实践,我应该把计算关键指标和分析的方法放在哪里?
- python - 遍历和提取树的节点时出现问题?
- kubernetes - 在 Kubernetes 上运行 Airflow 任务
- php - 在 PHP 中,为什么当我将 var 作为字符串转换为浮点数时,我会失去精度?
- python - 需要帮助确定用户是否使用 Python 回答“是”或“否”
- ruby - 使用 Ruby File.open 写入具有长文件名的外部文件
- javascript - 如何使用 React Hook useEffect 检查状态默认布尔值 false 是否更改为 true
- java - 寻求帮助 在 1.14 Minecraft 中设置粒子