首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

正则表达式找子字符串,该怎么解决

2012-01-18 
正则表达式找子字符串您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。我现在要抽一个字

正则表达式找子字符串
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。

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

感谢您的回复

[解决办法]
VB 里貌似木有正则.
[解决办法]

探讨
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。

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

感谢您的回复

[解决办法]
只要有需求,都能用VB来实现的。
用正则,也是一系列处理过程,只不过是系统帮你做了罢了。
[解决办法]
^.{3}相当于Left 3
.{3}$相当于Right 3
^(.{2})(.{3})的第二个子匹配相当于Mid 2 3
VB6调用VBScript控件msscript.ocx:
VB code
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 StringDim 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)      " + vbCrLfcodestr = codestr + "    Dim regEx, Match, Matches, RetStr, ii  " + vbCrLfcodestr = codestr + "    Dim nn,ss                              " + vbCrLfcodestr = codestr + "    nn=fix(ns)                             " + vbCrLfcodestr = codestr + "    if nn=ns then                          " + vbCrLfcodestr = codestr + "        ss=-1                              " + vbCrLfcodestr = codestr + "    else                                   " + vbCrLfcodestr = codestr + "        ss=(ns-nn)*10-1                    " + vbCrLfcodestr = codestr + "    end if                                 " + vbCrLfcodestr = codestr + "    Set regEx = New RegExp                 " + vbCrLfcodestr = codestr + "    regEx.Pattern    = patrn               " + vbCrLfcodestr = codestr + "    regEx.IgnoreCase = True                " + vbCrLfcodestr = codestr + "    regEx.Global     = True                " + vbCrLfcodestr = codestr + "    Set Matches = regEx.Execute(strng)     " + vbCrLfcodestr = codestr + "    ii=0                                   " + vbCrLfcodestr = codestr + "    For Each Match in Matches              " + vbCrLfcodestr = codestr + "        ii=ii+1                            " + vbCrLfcodestr = codestr + "        if ii=nn then                      " + vbCrLfcodestr = codestr + "            if ss=-1 then                  " + vbCrLfcodestr = codestr + "                RetStr=Match.Value         " + vbCrLfcodestr = codestr + "            else                           " + vbCrLfcodestr = codestr + "                RetStr=Match.SubMatches(ss)" + vbCrLfcodestr = codestr + "            end if                         " + vbCrLfcodestr = codestr + "            exit for                       " + vbCrLfcodestr = codestr + "        end if                             " + vbCrLfcodestr = codestr + "    Next                                   " + vbCrLfcodestr = codestr + "    RegExpTest = RetStr                    " + vbCrLfcodestr = codestr + "    Set regEx = Nothing                    " + vbCrLfcodestr = 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 = rtnstrEnd Function 


[解决办法]

探讨
比如
VB code

strSubString=left("AAAABBBBCCC",5)



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


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

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

[解决办法]
vb里可以引用正则表达式:
microsoft vbscript regular expressions 5.5
[解决办法]
<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 ...

热点排行