问个正则表达式

无·法 2009-09-29 12:04:29
Option Explicit

Private Sub Command1_Click()
Dim data$
data = "<a>http://www.baidu.com><span class=simple>s1</span></a>" & vbCrLf & _
"<a>http://www.sina.com><span class=simple>t2</span></a>" & vbCrLf & _
"<a>http://wx.txes.com><span class=simple>tr0</span></a>"

Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.Global = True
reg.ignorecase = True
reg.Pattern = "<a>.*><span class=simple>"

Dim matches
Set matches = reg.execute(data)

Dim match
For Each match In matches
Call MsgBox(match.Value)
Next
End Sub

请问上面如何取出特定的网址啊,我这样的直接显示整个字符串了,现在的做法是取出来再用字符串函数处理下,比较麻烦,我觉得可以一步到位的,各位帮帮忙,谢谢!
...全文
80 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
king06 2009-09-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lsftest 的回复:]
晕,csdn怎么老是喜欢帮人加空格。。4楼的pattern最前面也是没有空格的。
[/Quote]
O(∩_∩)O哈哈~,顺便学习一下~
s11ss 2009-09-30
  • 打赏
  • 举报
回复
不可以,因为?的作用是指定匹配是非贪婪的,就是尽量少的匹配,而不是默认的尽量多的匹配(会匹配到行尾)[Quote=引用 8 楼 sysdzw 的回复:]
引用 4 楼 lsftest 的回复:
Dim myRegExp
Set myRegExp = New RegExp
myRegExp.Pattern = " <a>(.*?)>"


谢谢各位,不过上面的这个去掉问号也是可以的吧?
[/Quote]
无·法 2009-09-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lsftest 的回复:]
Dim myRegExp
Set myRegExp = New RegExp
myRegExp.Pattern = " <a>(.*?)>"
[/Quote]

谢谢各位,不过上面的这个去掉问号也是可以的吧?
s11ss 2009-09-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lsftest 的回复:]
Dim myRegExp
Set myRegExp = New RegExp
myRegExp.Pattern = " <a>(.*?)>"
[/Quote]
study
lsftest 2009-09-29
  • 打赏
  • 举报
回复
晕,csdn怎么老是喜欢帮人加空格。。4楼的pattern最前面也是没有空格的。
lsftest 2009-09-29
  • 打赏
  • 举报
回复
Dim myRegExp
Set myRegExp = New RegExp
myRegExp.Pattern = "<a>(.*?)>"
s11ss 2009-09-29
  • 打赏
  • 举报
回复
reg.Pattern = "<a>([^>]+)>"
'第一个引号后面没有空格
s11ss 2009-09-29
  • 打赏
  • 举报
回复
Option Explicit


Private Sub Command1_Click()
Dim data$
data = "<a>http://www.baidu.com><span class=simple>s1</span></a>" & vbCrLf & _
"<a>http://www.sina.com><span class=simple>t2</span></a>" & vbCrLf & _
"<a>http://wx.txes.com><span class=simple>tr0</span></a>"

Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.Global = True
reg.ignorecase = True
reg.Pattern = "<a>([^>]+)>"

Dim matches
Set matches = reg.execute(data)

Dim match
For Each match In matches
Call MsgBox(match.submatches(0))
Next
End Sub
  • 打赏
  • 举报
回复
应该没有,不然他不会花时间写这些
http://www.vbforums.com/showthread.php?t=342995

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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