正则表达式找子字符串

gooore 2011-12-02 03:41:10
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。

我现在要抽一个字符串的子串,但是我想把想抽什么内容开放出来,用正则表达式实现,不知道可不可行。

感谢您的回复
...全文
232 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gooore 2011-12-07
  • 打赏
  • 举报
回复
楼上的你直接用DOM解析XML好了
我准备结贴了。

如果不知道该怎么解决,就开个新帖子吧

[Quote=引用 10 楼 polosa 的回复:]
<span class="title1">
<a href="http://www..........">
感谢有你 感恩节!!!!
</a>
</span>
<span class="title2">
QQ群:
<img src="111.img">

BBBBB 群
</a>
</span>
<span class="titl……
[/Quote]
polosa 2011-12-03
  • 打赏
  • 举报
回复
<span class="title1">
<a href="http://www..........">
感谢有你 感恩节!!!!
</a>
</span>
<span class="title2">
QQ群:
<img src="111.img">

BBBBB 群
</a>
</span>
<span class="title3" title="线上">
<img src="222.img">
在线
</span>

请问要如何使用正则表达式取出:title1,title2,title3 里面的内容,而且也要取出 title1 的网址并避开 title3,title3 内的 <img src ...
chinaboyzyq 2011-12-02
  • 打赏
  • 举报
回复
vb里可以引用正则表达式:
microsoft vbscript regular expressions 5.5
无·法 2011-12-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gooore 的回复:]
比如
VB code

strSubString=left("AAAABBBBCCC",5)



多少位不固定,字符串没有严格的规律。


引用 2 楼 sysdzw 的回复:
引用楼主 gooore 的回复:
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。

我现在要抽一个字符串的子串,但是我想把想抽什么内容开放出来,用正则表达式实现,……
[/Quote]'此代码由“正则测试工具 v1.1.35”自动生成,请直接调用TestReg过程
Private Sub TestReg()
Dim strData As String
Dim reg As Object
Dim matchs As Object, match As Object

strData = "AAAABBBBCCC"

Set reg = CreateObject("vbscript.regExp")
reg.Global = True
reg.IgnoreCase = False
reg.MultiLine = True
reg.Pattern = "^.{5}"
Set matchs = reg.Execute(strData)
For Each match In matchs
Debug.Print match.Value
Next
End Sub
赵4老师 2011-12-02
  • 打赏
  • 举报
回复
^.{3}相当于Left 3
.{3}$相当于Right 3
^(.{2})(.{3})的第二个子匹配相当于Mid 2 3
VB6调用VBScript控件msscript.ocx:
Function RegExpN(ptn, txt, n) As String
'Debug.Print "[" + RegExpN("[012]{8}", "mno_if22220101_and11000011_or00111100_xor10101010.txt", 1) + "]"
'[22220101]
'Debug.Print "[" + RegExpN("[012]{8}", "mno_if22220101_and11000011_or00111100_xor10101010.txt", 2) + "]"
'[11000011]
'Debug.Print "[" + RegExpN("[012]{8}", "mno_if22220101_and11000011_or00111100_xor10101010.txt", 3) + "]"
'[00111100]
'Debug.Print "[" + RegExpN("[012]{8}", "mno_if22220101_and11000011_or00111100_xor10101010.txt", 4) + "]"
'[10101010]
'Debug.Print "[" + RegExpN("[012]{8}", "mno_if22220101_and11000011_or00111100_xor10101010.txt", 5) + "]"
'[]
'Debug.Print "[" + RegExpN("[a-z]+(\d+)[_.]", "mno_if22220101_and11000011_or00111100_xor10101010.txt", 4) + "]"
'[xor10101010.]
'Debug.Print "[" + RegExpN("[a-z]+(\d+)[_.]", "mno_if22220101_and11000011_or00111100_xor10101010.txt", 4.1) + "]"
'[10101010]
'Debug.Print "[" + RegExpN("<abc>(.*)</abc>", "<html><ab>AB</ab><abc>ABC汉字</abc></html>", 1) + "]"
'[<abc>ABC汉字</abc>]
'Debug.Print "[" + RegExpN("<abc>(.*)</abc>", "<html><ab>AB</ab><abc>ABC汉字</abc></html>", 1.1) + "]"
'[ABC汉字]
Dim rtnstr As String
Dim codestr As String
rtnstr = ""
With ScriptControl1
' Set script language (VBScript is the default).
.Language = "VBScript"
' Set UI interaction (TRUE is the default).
.AllowUI = True
' Copy the script to the control.
'--------------------------------------------------------
codestr = ""
codestr = codestr + "Function RegExpTest(patrn, strng, ns) " + vbCrLf
codestr = codestr + " Dim regEx, Match, Matches, RetStr, ii " + vbCrLf
codestr = codestr + " Dim nn,ss " + vbCrLf
codestr = codestr + " nn=fix(ns) " + vbCrLf
codestr = codestr + " if nn=ns then " + vbCrLf
codestr = codestr + " ss=-1 " + vbCrLf
codestr = codestr + " else " + vbCrLf
codestr = codestr + " ss=(ns-nn)*10-1 " + vbCrLf
codestr = codestr + " end if " + vbCrLf
codestr = codestr + " Set regEx = New RegExp " + vbCrLf
codestr = codestr + " regEx.Pattern = patrn " + vbCrLf
codestr = codestr + " regEx.IgnoreCase = True " + vbCrLf
codestr = codestr + " regEx.Global = True " + vbCrLf
codestr = codestr + " Set Matches = regEx.Execute(strng) " + vbCrLf
codestr = codestr + " ii=0 " + vbCrLf
codestr = codestr + " For Each Match in Matches " + vbCrLf
codestr = codestr + " ii=ii+1 " + vbCrLf
codestr = codestr + " if ii=nn then " + vbCrLf
codestr = codestr + " if ss=-1 then " + vbCrLf
codestr = codestr + " RetStr=Match.Value " + vbCrLf
codestr = codestr + " else " + vbCrLf
codestr = codestr + " RetStr=Match.SubMatches(ss)" + vbCrLf
codestr = codestr + " end if " + vbCrLf
codestr = codestr + " exit for " + vbCrLf
codestr = codestr + " end if " + vbCrLf
codestr = codestr + " Next " + vbCrLf
codestr = codestr + " RegExpTest = RetStr " + vbCrLf
codestr = codestr + " Set regEx = Nothing " + vbCrLf
codestr = codestr + "End Function " + vbCrLf
'--------------------------------------------------------
.AddCode codestr
Dim oMod As Object
Set oMod = .Modules(GlobalModule)
rtnstr = oMod.Run("RegExpTest", ptn, txt, n)
Set oMod = Nothing
End With
RegExpN = rtnstr
End Function
-晴天 2011-12-02
  • 打赏
  • 举报
回复
只要有需求,都能用VB来实现的。
用正则,也是一系列处理过程,只不过是系统帮你做了罢了。
gooore 2011-12-02
  • 打赏
  • 举报
回复
我是说,可以外部定义取多少位的。

[Quote=引用 4 楼 gooore 的回复:]
比如

VB code


strSubString=left("AAAABBBBCCC",5)



多少位不固定,字符串没有严格的规律。



引用 2 楼 sysdzw 的回复:
引用楼主 gooore 的回复:
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。

我现在要抽一个字符串的子串,但是我想把想抽什么内容开放出来,……
[/Quote]
gooore 2011-12-02
  • 打赏
  • 举报
回复

比如

strSubString=left("AAAABBBBCCC",5)


多少位不固定,字符串没有严格的规律。


[Quote=引用 2 楼 sysdzw 的回复:]
引用楼主 gooore 的回复:
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。

我现在要抽一个字符串的子串,但是我想把想抽什么内容开放出来,用正则表达式实现,不知道可不可行。

感谢您的回复
给出具体事例。你目前用left之类的怎么操作的?
[/Quote]
gooore 2011-12-02
  • 打赏
  • 举报
回复
没有也没有关系,主要想要正则表达式。
我可以把代码转到.net上面去

[Quote=引用 1 楼 qianjin036a 的回复:]
VB 里貌似木有正则.
[/Quote]
无·法 2011-12-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 gooore 的回复:]
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。

我现在要抽一个字符串的子串,但是我想把想抽什么内容开放出来,用正则表达式实现,不知道可不可行。

感谢您的回复
[/Quote]给出具体事例。你目前用left之类的怎么操作的?
-晴天 2011-12-02
  • 打赏
  • 举报
回复
VB 里貌似木有正则.

7,785

社区成员

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

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