Vbs脚本添加空间,如果它找到像abc11adv到abc11 adv的字符串

时间:2014-08-10 12:27:02

标签: vbscript

您好我是vbs脚本的新手。我有一个文本文件,每行都有像

这样的语句
   minis1in use by bla bla  
   rit34in use by someone  
   atp34in use by someone2  

我想要一个vbs脚本将此文本文件转换为

   minis1 in use by bla bla  
   rit34 in use by someone  
   atp34 in use by someone2 

我找到了一个vbs脚本,但它替换了每一行中特定位置的字符串。但是我想在每一行的第一个字符串中搜索一个数字,而数字可能是一个数字,两个数字或三个数字后面应该给出空格。不用空格替换字符。

StrFileName = "C:\Users\Desktop\Scheduled\output.txt"

Const ForReading = 1    
Const ForWriting = 2
Dim objFSO
Dim objTF
Dim objRexex
Dim StrFileName
Dim strTxt

StrFileName = "C:\Users\Desktop\Scheduled\output.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile(StrFileName, ForReading)
Set objregex = CreateObject("vbscript.regexp")
strTxt = objTF.ReadAll
objTF.Close
With objregex
    .Global = True
    .MultiLine = True
    .Pattern = "^(.{6})[^-](.*)$"
    strTxt = .Replace(strTxt, "$1" & " " & "$2")
End With

Set objTF = objFSO.OpenTextFile(StrFileName, ForWriting)
objTF.Write strTxt
objTF.Close

2 个答案:

答案 0 :(得分:0)

也许非全局使用的模式“^(\ w +)(\ d +)(\ w +)”会做你想要的:

Option Explicit

Dim r : Set r = New RegExp
r.Global = False ' just the first
r.Pattern = "^(\w+)(\d+)(\w+)"
Dim s
For Each s In Split("minis1in use by+rit34adv use+atp34in use not34in use+not here1in use", "+")
    WScript.Echo s
    WScript.Echo r.Replace(s, "$1$2 $3")
    WScript.Echo
Next

输出:

cscript 25228592.vbs
minis1in use by
minis1 in use by

rit34adv use
rit34 adv use

atp34in use not34in use
atp34 in use not34in use

not here1in use
not here1in use

答案 1 :(得分:0)

如果“每一行都有类似”你所展示的内容的陈述,并且你确定这一点,那就让自己轻松一点:

strTxt = Replace(strTxt, "in use by ", " in use by ")