正则表达式找子字符串
您好,请问能不能用正则表达式实现VB里面 Left,Right,Mid这样的功能。
我现在要抽一个字符串的子串,但是我想把想抽什么内容开放出来,用正则表达式实现,不知道可不可行。
感谢您的回复
[解决办法]
VB 里貌似木有正则.
[解决办法]
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
[解决办法]