当前位置: 首页 > news >正文

VBA之正则表达式(43)-- 从网页中提取指定数据

实例需求:由网页中提取下图中颜色标记部分内容,网页中其他部分与此三行格式相同。

在这里插入图片描述


方法1

Sub Demo()Dim objRegex As ObjectDim inputString As StringDim objMatches As ObjectDim objMatch As ObjectSet objRegex = CreateObject("VBScript.RegExp")objRegex.Pattern = "id=[^>]+|[^>]+&#[^<]+"objRegex.Global = TrueinputString = Cells(1,1).ValueSet objMatches = objRegex.Execute(inputString)For Each objMatch In objMatchesDebug.Print objMatchNext objMatch
End Sub

【代码解析】
第6行代码创建正则对象。
第7行代码设置正则匹配模式。

正则表达式说明
id=[^>]+匹配以id=开头,后面跟一个或者多个除>之外的任意字符
[^>]+&#[^<]+匹配以一个或者多个除>之外的任意字符开头,后面为&#,之后为一个或者多个除<之外的任意字符

第8行代码设置正则全局匹配。
第9~11行代码将待处理字符串保存在变量中。
第12行代码执行正则匹配。
第13~15行代码循环遍历匹配结果,第14行代码输出到立即窗口,结果如下图所示。
在这里插入图片描述


方法2

使用常规正则匹配模式,此正则模式比较容易理解,不再讲解其含义。
方法1中灵活使用了字符集[^……],限制非某字符,也就实现了以该字符为边界的效果,其匹配模式字符串更简洁。

Sub Demo2()Dim objRegex As ObjectDim inputString As StringDim objMatches As ObjectDim objMatch As ObjectSet objRegex = CreateObject("VBScript.RegExp")objRegex.Pattern = "(id=""[^""]+"").*?<text.*?>(.*?)<\/text>"objRegex.Global = TrueinputString = Cells(1,1).ValueSet objMatches = objRegex.Execute(inputString)For Each objMatch In objMatchesDebug.Print "ID: " & objMatch.SubMatches(0)Debug.Print "Text: " & objMatch.SubMatches(1)Debug.Print "----------------"Next objMatch
End Sub

在这里插入图片描述


示例数据

<g class="node" transform="translate(1980, 861.0625)" id="node-946327"><rect width="120" height="36" transform="translate(-60,-18)" rx="18" ry="18" stroke="darkseagreen" stroke-width="2" style="fill: rgb(255, 255, 255);"></rect><circle r="18" transform="translate(-42,0)" style="fill: url(&quot;#image-person-old&quot;);"></circle><text transform="translate(18,0)" y="4" text-anchor="middle" style="fill-opacity: 1; font: 12px sans-serif;">Байтайла&#1179;</text><text transform="translate(18,0)" y="10" text-anchor="middle" style="fill-opacity: 1; font: 8px sans-serif;"></text></g><g class="node" transform="translate(1980, 907.0625)" id="node-946328"><rect width="120" height="36" transform="translate(-60,-18)" rx="18" ry="18" stroke="darkseagreen" stroke-width="2" style="fill: rgb(255, 255, 255);"></rect><circle r="18" transform="translate(-42,0)" style="fill: url(&quot;#image-person-old&quot;);"></circle><text transform="translate(18,0)" y="4" text-anchor="middle" style="fill-opacity: 1; font: 12px sans-serif;">Байто&#1171;ай</text><text transform="translate(18,0)" y="10" text-anchor="middle" style="fill-opacity: 1; font: 8px sans-serif;"></text></g><g class="node" transform="translate(1980, 953.0625)" id="node-946329"><rect width="120" height="36" transform="translate(-60,-18)" rx="18" ry="18" stroke="darkseagreen" stroke-width="2" style="fill: rgb(255, 255, 255);"></rect><circle r="18" transform="translate(-42,0)" style="fill: url(&quot;#image-person-old&quot;);"></circle><text transform="translate(18,0)" y="4" text-anchor="middle" style="fill-opacity: 1; font: 12px sans-serif;">Ораз&#1241;л&#1110;</text><text transform="translate(18,0)" y="10" text-anchor="middle" style="fill-opacity: 1; font: 8px sans-serif;"></text></g>
http://www.lryc.cn/news/194529.html

相关文章:

  • Elucidating the Design Space of Diffusion-Based Generative Models 阅读笔记
  • 计算机网络 | 传输层
  • Android 13 隐私权限和安全变更之通知
  • docker-compose安装和使用(自启、redis、mysql、rabbitmq、activemq、es、nginx、java应用)
  • dll文件缺失,ps,pr无法打开,游戏运行不了如何解决
  • 前后端数据导入导出Excel
  • RackNerd 圣何塞 VPS 测评
  • php74 安装sodium
  • 优思学院:想成为质量工程师?了解质量工程师职责和能力是关键!
  • Rollup failed to resolve import
  • 基于VScode 使用plantUML 插件设计状态机
  • 2023年中国汽车智能工厂市场规模不断增大,智能化已成趋势[图]
  • cola架构:一种扩展点的实现思路浅析
  • Thread常用API
  • 系列九、Redis的发布订阅
  • 《TypeScript》系列之对比JavaScript,TypeScript的优势
  • Notepad++使用技巧
  • React 中报Type error: Could not find a declaration file for module ‘qs‘.(已解决)
  • 【Linux】:Linux环境与版本
  • PNG转EPS,包括Latex导入
  • Visual Studio 2022 cmake编译 PP-OCRv4
  • 学习笔记|串口与PC通信的接线|移植驱动程序|串口通信实战|STC32G单片机视频开发教程(冲哥)|第二十一集(上):串口与PC通信
  • 关于Mybaits缓存....
  • Vue axios调用springboot接口获取数据库数据并显示到网页
  • 12-bean创建流程3
  • volatile关键字 和 i = i + 1过程
  • ubuntu20 安装 cmake 3.27
  • faster lio 回环 加入GTSAM优化的记录
  • 深入剖析 深度学习中 __init()__函数和forward()函数
  • BUUCTF学习(一):SQL注入,万能密码