本帖最后由 apang 于 2014-3-9 23:41 编辑
- Dim strPrompt, Input
- strPrompt = "1:指定顺数单行" & vbLf & "2:指定顺数多行" & vbLf & _
- "3:指定倒数单行" & vbLf & "4:指定倒数多行" & vbLf & _
- "5:删除指定内容" & vbLf & vbLf & "输入[1-5]"
- Input = GetInput(strPrompt, "", "1", "[1-5]")
-
- Dim s, Pattern, a, b, s1, s2, ar, ss
- If Input = "1" Then
- strPrompt = "输入指定行:"
- s = GetInput(strPrompt,"指定顺数单行","1","[1-9]\d*")
- Pattern = "((.*\n){" & s-1 & "}).*\n([\s\S]*)"
-
- ElseIf Input = "2" Then
- strPrompt = "输入起始行和结束行,空格隔开:"
- s = GetInput(strPrompt,"指定顺数多行","1 2","[1-9]\d* +[1-9]\d*")
- a = Left(s, InStr(s, " ")-1)
- b = Right(s, Len(s) - InStrRev(s, " "))
- Pattern = "^((.*\n){" & a-1 & "})(.*\n){" & b-a+1 & "}([\s\S]*)$"
-
- ElseIf Input = "3" Then
- strPrompt = "输入指定行:"
- s = GetInput(strPrompt,"指定倒数单行","1","[1-9]\d*")
- Pattern = "^([\s\S]*?)(.*\n)((.*\n){" & s-1 & "})$"
-
- ElseIf Input = "4" Then
- strPrompt = "输入起始行和结束行,空格隔开:"
- s = GetInput(strPrompt,"指定倒数多行","1 2","[1-9]\d* +[1-9]\d*")
- a = Left(s, InStr(s, " ")-1)
- b = Right(s, Len(s) - InStrRev(s, " "))
- Pattern = "^([\s\S]*?)(.*\n){" & b-a+1 & "}((.*\n){" & a-1 & "})$"
-
- Else
- strPrompt = "1:删除指定字符之前的内容" & vbLf & _
- "2:删除指定字符之间的内容" & vbLf & _
- "3:删除指定字符之后的内容" & vbLf & vbLf & "输入[1-3]"
- ar = Array("\",".","*","?","$","(",")","^","|","+","{","[")
- ss = GetInput(strPrompt,"删除指定内容","1","[1-3]")
-
- If ss = "1" Then
- strPrompt = "输入指定字符:" & vbLf & vbLf & _
- "最大匹配不删除指定字符"
- s = GetInput(strPrompt,"删除指定字符之前的内容","A",".+")
- For i = 0 to UBound(ar)
- s = Replace(s, ar(i), "\" & ar(i))
- Next
- Pattern = "[\s\S]*(" & s & ")"
-
- ElseIf ss = "2" Then
- strPrompt = "输入起始字符:" & vbLf & vbLf & _
- "最小匹配不删除起始、结束字符"
- s1 = GetInput(strPrompt,"删除指定字符之间的内容","A",".+")
- strPrompt = "输入结束字符:"
- s2 = GetInput(strPrompt,"删除指定字符之间的内容","B",".+")
- For i = 0 to UBound(ar)
- s1 = Replace(s1, ar(i), "\" & ar(i))
- s2 = Replace(s2, ar(i), "\" & ar(i))
- Next
- Pattern = "(" & s1 & ")[\s\S]*?(" & s2 & ")"
-
- Else
- strPrompt = "输入指定字符:" & vbLf & vbLf & _
- "最大匹配不删除指定字符"
- s = GetInput(strPrompt,"删除指定字符之后的内容","A",".+")
- For i = 0 to UBound(ar)
- s = Replace(s, ar(i), "\" & ar(i))
- Next
- Pattern = "(" & s & ")[\s\S]*"
- End If
- End If
-
- Dim fso, file, txt
- Set fso = CreateObject("Scripting.FileSystemObject")
- For Each file in fso.GetFolder(".").Files
- If LCase(Right(file.Name,4)) = ".txt" Then
- txt = fso.OpenTextFile(file, 1).ReadAll
- If Right(txt,2) <> vbCrLf Then txt = txt & vbCrLf
- fso.OpenTextFile(file, 2).Write RegEx(txt, Pattern)
- End If
- Next
-
- MsgBox "OK"
-
- Function RegEx(str, Pattern)
- Dim re
- Set re = New RegExp
- re.Pattern = Pattern
- If Input = "5" Then
- re.IgnoreCase = True
- If ss = "2" Then
- re.Global = True
- RegEx = re.Replace(str,"$1$2")
- Else RegEx = re.Replace(str,"$1")
- End If
- ElseIf Input = "2" Then RegEx = re.Replace(str,"$1$4")
- Else RegEx = re.Replace(str,"$1$3")
- End If
- End Function
-
- Function GetInput(strPrompt, strTitle, strDefault, strPattern)
- Dim Input1
- Do while true
- Input1 = InputBox(strPrompt, strTitle, strDefault)
- If IsEmpty(Input1) Then WScript.Quit
- If IsValidInput(CStr(Input1),strPattern) Then Exit Do
- Loop
- GetInput = Input1
- End Function
-
- Function IsValidInput(strInput, Pattern)
- Dim re
- Set re = New RegExp
- re.Pattern = "^" & Pattern & "$"
- If re.Test(strInput) Then IsValidInput = true
- End function
复制代码
|