正则表达式匹配RTF中的图片

firehou 2011-06-29 05:26:22
RTF中的图片,想查找出来 用正则表达式能匹配出来吗,或其他办法

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}
\viewkind4\uc1\pard\lang2052\f0\fs18 sdfds\f1{\object\objemb{\*\objclass Paint.Picture}\objw360\objh360{\*\objdata
01050000
.....
00000000
}{\result{\pict\wmetafile0
}}}
\par
}
...全文
134 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingdom_0 2011-06-30
  • 打赏
  • 举报
回复
既然没有什么规律,那就字符串截取来处理。
正则是为了复杂问题简单化。简单问题复杂化不是给自己找事儿干呢么
porschev 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 firehou 的回复:]

老办法实现
C# code

string rtf = base.Rtf;
string DestStr = "";

while (rtf.Contains(@"{\object\objemb{\*\objclass Paint.Picture}"))
{
DestStr += rtf……
[/Quote]

用这办法还好些。。。正则根本看不出什么规律来。。
firehou 2011-06-30
  • 打赏
  • 举报
回复
老办法实现

string rtf = base.Rtf;
string DestStr = "";

while (rtf.Contains(@"{\object\objemb{\*\objclass Paint.Picture}"))
{
DestStr += rtf.Substring(0, rtf.IndexOf(@"{\object\objemb{\*\objclass Paint.Picture}"));
rtf = rtf.Substring(rtf.IndexOf(@"{\object\objemb{\*\objclass Paint.Picture}") + @"{\object\objemb{\*\objclass Paint.Picture}".Length);
string temp = rtf.Substring(rtf.IndexOf(@"{\result{\pict\wmetafile0") + @"{\result{\pict\wmetafile0".Length + 6);
DestStr += "[##img##]";
rtf = rtf.Substring(rtf.IndexOf(@"{\result{\pict\wmetafile0") + @"{\result{\pict\wmetafile0".Length + 6);
}
return DestStr + rtf;
firehou 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用楼主 firehou 的回复:]
RTF中的图片,想查找出来 用正则表达式能匹配出来吗,或其他办法

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}
\viewkind4\uc1\pard\lang2052\……
[/Quote]
红色对就的部分相当于一对,省略号的内容不同,要匹配的内容为
{\object\objemb{\*\objclass Paint.Picture}任意字符{\result{\pict\wmetafile0
}}}



q107770540 2011-06-29
  • 打赏
  • 举报
回复
没看出匹配规则是什么
sxldfang 2011-06-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace sxLdfang
{
class Program
{
static void Main(string[] args)
{
string html = @"{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}
\viewkind4\uc1\pard\lang2052\f0\fs18 sdfds\f1{\object\objemb{\*\objclass Paint.Picture}\objw360\objh360{\*\objdata
01050000
.....
00000000
}{\result{\pict\wmetafile0
}}}\par
}";
string pattern = @"(?is)(?<={\\object\\objemb{\\\*\\objclass Paint\.Picture}\\objw\d+\\objh\d+{\\\*\\objdata).*(?=}{\\result{\\pict\\wmetafile0
}}}\\par)";
MatchCollection mc = Regex.Matches(html, pattern);
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
}
}


运行结果:

01050000
.....
00000000


huangwenquan123 2011-06-29
  • 打赏
  • 举报
回复
有虾米规律?

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧