本帖最后由 apang 于 2014-6-18 11:29 编辑
回复 14# zhanglei1371 - Dim strCase, fso, f, str, n, ar, re, m
- strCase = "零一二三四五六七八九十百千"
- Set fso = CreateObject("Scripting.FileSystemObject")
- For Each f in fso.GetFolder("Test").Files
- If LCase(Right(f, 4)) = ".txt" Then str = str & f.Name & vbCrLf
- Next
-
- n = 0 : ReDim ar(n)
- Set re = New RegExp
- re.Pattern = "^第([" & strCase & "]+)[章节].*?\n"
- re.Global = True
- re.MultiLine = True
- For Each m in re.Execute(str)
- ReDim PreServe ar(n)
- ar(n) = ConvertCase(m.SubMatches(0)) & " " & m
- n = n + 1
- Next
-
- call qSort(0, UBound(ar))
- fso.CreateTextFile("List.txt", true).Write RegEx(Join(ar, ""))
-
- MsgBox "OK"
-
- ''大写数字转成小写数字
- Function ConvertCase(s)
- Dim i, s1, ss
- If Left(s, 1) = "十" Then s = "一" & s
- ''首位“十”改成“一十”
- For i = 1 to Len(s)
- s1 = Mid(s, i, 1)
- ''循环截取变量s值的第i位,赋值给s1
- If InStr(strCase, s1) > 10 Then
- ''变量s1值位于strCase值的第10位以上(十百千),改成形如 *10^2+
- ss = ss & "*10^" & (InStr(strCase, s1)-10) & "+"
- Else ss = ss & (InStr(strCase, s1)-1)
- ''否则改成strCase值中对应的位值数
- End If
- Next
- ConvertCase = Right("0000" & eval(ss & "+0"), 5)
- ''计算并凑成5位数字
- End Function
-
- ''排序
- Sub qSort(low, high)
- Dim i, j, pos, tmp
- If low > high Then Exit Sub
- i = low : j = high
- pos = ar(low)
- while i <> j
- while i < j and ar(j) >= pos
- j = j - 1
- wend
- while i < j and ar(i) <= pos
- i = i + 1
- wend
- tmp = ar(i) : ar(i) = ar(j) : ar(j) = tmp
- wend
- ar(low) = ar(i) : ar(i) = pos
- qSort low, i-1
- qSort i+1, high
- End Sub
-
- ''去掉前面的5位数字
- Function RegEx(s)
- Dim reg
- Set reg = New RegExp
- reg.Pattern = "^\d+ "
- reg.Global = true
- reg.MultiLine = true
- RegEx = reg.Replace(s, "")
- End Function
复制代码 改一下,代码变长了,但效率比原来要好 |