ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 2131|回复: 7

[求助] 求正则表达式

[复制链接]

TA的精华主题

TA的得分主题

发表于 2016-10-28 21:05 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
求用正则表达式从网页代码中提取数据:
网页数据格式如下 :
<tr height=41 style='mso-height-source:userset;height:30.75pt'>
  <td height=41 class=xl6423934 style='height:30.75pt'></td>
  <td class=xl6523934 width=99 style='width:74pt'>行政区划代码</td>
  <td class=xl6623934 width=233 style='width:175pt'><span
  style='mso-spacerun:yes'>  </span>单位名称</td>
  <td class=xl6423934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl1523934></td>
  <td class=xl1523934></td>
</tr>
<tr height=19 style='mso-height-source:userset;height:14.25pt'>
  <td height=19 class=xl6423934 style='height:14.25pt'></td>
  <td class=xl6923934>110000</td>
  <td class=xl6923934>北京市</td>
  <td class=xl6423934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl1523934></td>
  <td class=xl1523934></td>
</tr>
<tr height=19 style='mso-height-source:userset;height:14.25pt'>
  <td height=19 class=xl6423934 style='height:14.25pt'></td>
  <td class=xl6923934>110101</td>
  <td class=xl6923934><span style='mso-spacerun:yes'>    </span>东城区</td>
  <td class=xl6423934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl1523934></td>
  <td class=xl1523934></td>
</tr>
<tr height=19 style='mso-height-source:userset;height:14.25pt'>
  <td height=19 class=xl6423934 style='height:14.25pt'></td>
  <td class=xl6923934>110102</td>
  <td class=xl6923934><span style='mso-spacerun:yes'>    </span>西城区</td>
  <td class=xl6423934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl6823934></td>
  <td class=xl1523934></td>
  <td class=xl1523934></td>
</tr>

提取到的数据大致为两列:
行政区划代码 单位名称
110000 北京市
110101 东城区
110102 西城区

代码大致为这样:
  •    Set reg = CreateObject("vbscript.regexp")
  •    Set dic = CreateObject("Scripting.Dictionary")
  •     reg.Pattern = "这里不会用表达式"
  •     reg.Global = True
  •     Set mh = reg.Execute(s)
  •     For Each k In mh
  •         dic.add k.SubMatches(0),  k.SubMatches(1)
  •     Next



TA的精华主题

TA的得分主题

发表于 2016-10-28 23:53 | 显示全部楼层
猜一个:
”<td class=xl6923934>(\d{6})</td><td class=xl6923934>.*([一-龥]+)</td>"

TA的精华主题

TA的得分主题

发表于 2016-10-29 01:11 | 显示全部楼层
正则参考学习.rar (11.78 KB, 下载次数: 27)

看看可以不

TA的精华主题

TA的得分主题

发表于 2016-10-29 01:12 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
(\w+)</td>\s+<td class=xl6923934>(<span style='mso-spacerun:yes'>    </span>)?([一-龢]+)

TA的精华主题

TA的得分主题

发表于 2016-10-29 16:35 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-10-29 18:29 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
未忘初心 发表于 2016-10-29 01:12
(\w+)\s+(    )?([一-龢]+)

测试附件正确,但真正网页数据中xl6923934是不一定相同的

TA的精华主题

TA的得分主题

发表于 2016-10-29 21:32 | 显示全部楼层
  1. Sub Test()
  2.     ss = Cells(1, 1)
  3.     Set reg = CreateObject("VBScript.RegExp")
  4.     With reg
  5.         .Global = True
  6.         .Pattern = ">(\d+|[一-龡]+)<"
  7.         Set mt = .Execute(ss)
  8.         For i = 0 To mt.Count - 1
  9.           t = (i + 1) / 2
  10.            If t = Int(t) Then
  11.              Cells(t, 2) = mt(i - 1).submatches(0)
  12.              Cells(t, 3) = mt(i).submatches(0)
  13.             End If
  14.         Next i
  15.     End With
  16. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2018-3-20 19:20 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-25 23:02 , Processed in 0.034061 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表