html中正则匹配img
1.正则匹配html中的img标签,取出img的url并进行图片文件下载;
/// <summary>
/// 将image标签的src属性的url替换为base64
/// </summary>
/// <param name="questionHtml"></param>
/// <returns>返回替换imgurl后的questionHtml</returns>
public string GetBase64ImgHtml(string questionHtml)
{
//获取<question></question>标签[获取某个标签时的表达式]
// Regex regQuestion = new Regex(@"<question\b*[^<>]*?\b[\s\S]*>([\s\S]*)</question>", RegexOptions.IgnoreCase); string retHtml = string.Empty;
//Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
//去掉分组中的 \s 防止图片的链接中含有空格导致匹配的url不全的问题
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
// 搜索匹配的字符串
MatchCollection matches = regImg.Matches(questionHtml);
var list = new List<string>(); // 取得匹配项列表,并逐一替换成imgUrl
foreach (Match match in matches)
{
try
{
string imgUrl = match.Groups["imgUrl"].Value;
string imgType = imgUrl.Substring(imgUrl.LastIndexOf(".") + );
WebClient webClient = new WebClient();
Byte[] imgBytes = webClient.DownloadData(imgUrl);
string imgBase64Data = Convert.ToBase64String(imgBytes);
questionHtml = questionHtml.Replace(imgUrl, $"data:image/{imgType};base64,{imgBase64Data}");
}
catch (Exception ex)
{
continue;
}
} return questionHtml;
}
通过以上方法,就可以轻松将html中img标签转换为base64;
2.html中img标签中的base64转换为url
其实处理的思路都是一样的,正则匹配base64 的img也都基本一致(<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgData>[^""'<>]*)[^<>]*?/?[\s\t\r\n]*>)。但是此处还是值得记录一下。获取到img标签中的base64 字符串后,将其转存为本地图片的过程中,部分png格式图片转存失败,在用内存流初始化 BitMap 对象的时,一直报“参数无效错误”,但是用该种方式转存Jpg图片完全OK。
.通过这种方式,部分 png 图片转存时会报错:参数无效
var bytes = Convert.FromBase64String(base64Str);
using(var ms = new System.IO.MemoryStream(bytes, true)){
//var bitmap = new Bitmap(ms);
var bitmap =Image.FromStream();
bitmap.Save(imgPath);
bitmap.Dispose();
ms.Close();
} .直接改成将字节写入文件的方式处理;解决问题。
var bytes = Convert.FromBase64String(base64Str);
File.WriteAllBytes(imgPath, bytes);
html中正则匹配img的更多相关文章
- JS中正则匹配的三个方法match exec test的用法
javascript中正则匹配有3个方法,match,exec,test: match是字符串的一个方法,接收一个RegExp对象做为参数: match() 方法可在字符串内检索指定的值,或找到一个或 ...
- 关于php中正则匹配包括换行符在内的任意字符的问题总结
要使用正则匹配任意字符的话,通常有以下几种方法,这里我分别对每一种方法在使用的过程中做一个总结: 第一种方式:[.\n]*? 示例 ? PHP preg_match_all('/<div cla ...
- Python中正则匹配使用findall时的注意事项
在使用正则搜索内容时遇到一个小坑,百度搜了一下,遇到这个坑的还不少,特此记录一下. 比如说有一个字符串 "123@qq.comaaa@163.combbb@126.comasdf111@a ...
- Python中正则匹配使用findall,捕获分组(xxx)和非捕获分组(?:xxx)的差异
转自:https://blog.csdn.net/qq_42739440/article/details/81117919 下面是我在用findall匹配字符串时遇到的一个坑,分享出来供大家跳坑. 例 ...
- python中正则匹配之re模块
Python中正则表达式 re:re是提供正则表达式匹配操作的模块 一.什么是正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某个模式匹配,Python 自1.5版本起 ...
- JavaScript 中正则匹配时结果不一致的问题
创建示例项目 考察如下场景,我们有个输入框组件,输入时同时进行校验. interface IInputProps { label: string; } function Input({ label } ...
- grep中正则匹配的使用
如要匹配Computer或computer两个单词,可做如下操作: [Cc]mputer “.”允许匹配ASCII集中任意字符,或为字母,或为数字. 使用\{\}匹配模式结果出现的次数 匹配字母A出现 ...
- Java中正则匹配性能测试
工作中经常会用到在文本中每行检索某种pattern,刚才测试了三种方式,发现实际性能和预想的有区别 方式1: 直接字符串的matches方法,[string.matches("\\d+&qu ...
- JS中正则匹配开头不带空格,结尾也不带空格的字符串
在做项目的时候,要求限制SSID的长度.以及开头和结尾不能是空格. var reg = /^\S.{0,30}\S$/ "$$$ $$".match(reg); ==> ...
随机推荐
- The first one spawns an additional process forwarding requests to a series of workers (think about it as a form of shield, at the same level of apache or nginx), while the second one sets workers to n
Things to know (best practices and “issues”) READ IT !!! — uWSGI 2.0 documentationhttps://uwsgi-docs ...
- 前端知识点回顾——koa和模板引擎
koa 基于Node.js的web框架,koa1只兼容ES5,koa2兼容ES6及以后. const Koa = requier("koa"); const koa = new K ...
- Vue tree自定义事件注意点
<template> <div id="Tree_ElementTree" style="height: 100%;"> <el- ...
- java中 label 配合 break continue 使用方法
转 https://www.jianshu.com/p/7954b61bc6ee java中 label 配合 break continue 使用的其实是比较少的. 这种做法在业务代码中比较少见. 在 ...
- [服务器时区问题]PHP Warning: strftime(): It is not safe to rely on the system's timezone set
PHP Warning: strftime(): It is not safe to rely on the system's timezone set 当运行一些程序时,在httpd日志中会有如下警 ...
- MLN Alchemy
1. 前言: 本文主要参考Alchemy Tutorial, washington主页上挂出的所有Alchemy项目(包括Alchemy1.0, Alchemy2.0, AlchemyLite)都无法 ...
- Nginx使用默认配置启动异常处理
Ps1:错误问题:nginx: [error] OpenEvent("Global\ngx_reload_5988") failed (2: The system cannot f ...
- 解决Linux:Too many levels of symbolic links
Too many levels of symbolic links 解决:创建链接时使用绝对路径
- LAG函数实现环比
,)OVER(ORDER BY 年月) 环比金额 from( 年, 季度, 年月 ,SUM(金额本位币) 金额 FROM ( SELECT * FROM [dbo].[T_output] ) cb_v ...
- 使用 pyenv 可以在一个系统中安装多个python版本
Installl related yum install readline readline-devel readline-static -y yum install openssl openssl- ...