是的,就楼主的需求来看,我只是提供了1~len(s)去isnumberic(xx)或者xx like "[1-3]"这两种方法之外的方法。而做上面代码包含发帖所花的时间就几秒而已。代码生产率和运行效率都比原生的字符串处理函数强,何乐而不为呢?
谢谢大家的提醒,特别是函数的提醒,以前没用过,我自己做出来了 Sub tt() Dim str As String Dim postion As String Dim length As Integer str = "中国1-3号" For i = 1 To Len(str) If IsNumeric(Mid(str, i, 1)) Then postion = postion & CStr(i) End If Next i length = (Right(postion, 1) - Left(postion, 1)) + 1 MsgBox Mid(str, Left(postion, 1), length) End Sub 恩,正则表达式以前也用过,只是简单的分一分,所以用这个有点开销大了点。
If IsNumeric(Mid(str, i, 1)) Then也可以写成 If Mid(str, i, 1) like "[0-9]" Then
Private Sub TestReg()
Dim strData As String
Dim reg As Object
Dim matchs As Object, match As Object strData = "[问题点数:40分]" & vbCrLf & _
" temp12_3" & vbCrLf & _
" (temp12_3)" & vbCrLf & _
" 等 级:" & vbCrLf & _
" 结帖率:85.71%" & vbCrLf & _
" " & vbCrLf & _
"楼主发表于:2011-04-11 22:21:34" & vbCrLf & _
"如:""XXX路123号""中的123怎么提取." Set reg = CreateObject("vbscript.regExp")
reg.Global = True
reg.IgnoreCase = True
reg.MultiLine = True
reg.Pattern = "\d+"
Set matchs = reg.Execute(strData)
For Each match In matchs
'Debug.Print match.Value
Debug.Print match.SubMatches(0)
Next
End Sub输出:
1.40
2.12
3.3
4.12
5.3
6.85
7.71
8.2011
9.04
10.11
11.22
12.21
13.34
14.123
15.123
Sub tt()
Dim str As String
Dim postion As String
Dim length As Integer
str = "中国1-3号"
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then
postion = postion & CStr(i)
End If
Next i
length = (Right(postion, 1) - Left(postion, 1)) + 1
MsgBox Mid(str, Left(postion, 1), length)
End Sub
恩,正则表达式以前也用过,只是简单的分一分,所以用这个有点开销大了点。
If Mid(str, i, 1) like "[0-9]" Then