一个比较难的问题
在一个TEXT里面输入字符。比如:1in2out3in4out 或2in5out 等不确定长度的内容,但只有in和out 两种要求读取所有in前面的数字 以及读取所有out前面的数字
例如:1in2out3in4out 那么in前面的数字应该是1和3,out前面的数字应该是2和4
该怎么写呢。 --------------------编程问答-------------------- 用搜索"in"或"out"进行倒退得到字符数字,再cint()转 --------------------编程问答-------------------- Option Explicit
Private Sub Command1_Click()
Dim astr As String
Dim bstr As String
Dim strc As String
astr = "1in2out3in4out"
bstr = "in"
strc = "out"
Debug.Print AAA(astr, bstr)
Debug.Print AAA(astr, strc)
End Sub
Function AAA(ABB As String, ACC As String) As String
Dim i As Integer
AAA = ""
i = 1
Do Until i = 0
i = InStr(i, ABB, ACC)
If i <> 0 Then
AAA = AAA & "-" & Mid(ABB, (i - 1), 1)
i = i + 1
End If
Loop
End Function --------------------编程问答-------------------- Dim strTest As String, strTmp() As String, i As Integer
strTest = "1in2out3in1in2out4out1in2out3in4out" '测试字符串
'取out前面的数字,并输出在立即窗口
strTmp = Split(strTest, "out")
For i = 0 To UBound(strTmp)
If strTmp(i) <> "" Then Debug.Print Left(strTmp(i), 1)
Next --------------------编程问答--------------------
'text1 输入1in2out3in4out、text2 输出 in、text3 输出 out--------------------编程问答--------------------
private command1_click()
dim s as string, a() as string,i as long
text2=vbnullstring
text3=vbnullstring
s = replace(text1, "in","i,")
s = replace(s, "out", "o,")
a = split(s,",")
for i=0 to ubound(a)
select case right(a(i),1)
case "i"
text2.seltext = left(a(i),len(a(i))-1) & ","
case "o"
text3.seltext = left(a(i),len(a(i))-1) & ","
end select
next
end sub
Option Explicit
'引用Microsoft VBScript Regular Expressions 5.5
Function TestRegExp(myPattern As String, myString As String)
'Create objects.
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String
' Create a regular expression object.
Set objRegExp = New RegExp
'Set the pattern by using the Pattern property.
objRegExp.Pattern = myPattern
' Set Case Insensitivity.
objRegExp.IgnoreCase = True
'Set global applicability.
objRegExp.Global = True
'Test whether the String can be compared.
If (objRegExp.Test(myString) = True) Then
'Get the matches.
Set colMatches = objRegExp.Execute(myString) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
RetStr = RetStr & Left(objMatch.Value, 1) & ","
Next
Else
RetStr = "String Matching Failed"
End If
TestRegExp = RetStr
End Function
Private Sub Command1_Click()
MsgBox "in:" & (TestRegExp(".in", "1in2out3in1in2out4out1in2out3in4out"))
MsgBox "out:" & (TestRegExp(".out", "1in2out3in1in2out4out1in2out3in4out"))
End Sub
补充:VB , 基础类