Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 无法增加正则表达式匹配的组1中捕获的数字_Regex_Vbscript - Fatal编程技术网

Regex 无法增加正则表达式匹配的组1中捕获的数字

Regex 无法增加正则表达式匹配的组1中捕获的数字,regex,vbscript,Regex,Vbscript,我需要将文本文件中的所有数字(不是单词的一部分)增加1。我尝试使用该模式捕获所有这些数字,但无法在文件中增加(向其中添加1) 输入 text1 1 5 7 hello world 5. This is Samurai 10 not samurai10. text2 text1 2 6 8 hello world 6. This is Samurai 11 not samurai10. text2 const forReading = 1 set objFso = create

我需要将文本文件中的所有数字(不是单词的一部分)增加1。我尝试使用该模式捕获所有这些数字,但无法在文件中增加(向其中添加1)

输入

text1
    1 5 7
hello world 5. This is Samurai 10 not samurai10.
text2
text1
    2 6 8
hello world 6. This is Samurai 11 not samurai10.
text2
const forReading = 1
set objFso = createObject("scripting.filesystemobject")
strFilePath = split(wscript.scriptFullName,wscript.scriptName)(0) & "haha.txt"
set objFile = objFso.openTextFile(strFilePath, forReading)
set objReg = new RegExp
With objReg
    .Global = True
    .ignoreCase = true
    .multiline = true
    .pattern = "\b(\d+)\b"
End With

    strTemp = objFile.readAll()
    strTemp = objReg.replace(strTemp,cint("$1")+1)      '<--Here, I am getting the "Type mismatch 'Cint'" error. I just wanted to increment the number which was captured in Group 1
    msgbox strTemp

set objFile = Nothing
set objFso = Nothing
预期产出

text1
    1 5 7
hello world 5. This is Samurai 10 not samurai10.
text2
text1
    2 6 8
hello world 6. This is Samurai 11 not samurai10.
text2
const forReading = 1
set objFso = createObject("scripting.filesystemobject")
strFilePath = split(wscript.scriptFullName,wscript.scriptName)(0) & "haha.txt"
set objFile = objFso.openTextFile(strFilePath, forReading)
set objReg = new RegExp
With objReg
    .Global = True
    .ignoreCase = true
    .multiline = true
    .pattern = "\b(\d+)\b"
End With

    strTemp = objFile.readAll()
    strTemp = objReg.replace(strTemp,cint("$1")+1)      '<--Here, I am getting the "Type mismatch 'Cint'" error. I just wanted to increment the number which was captured in Group 1
    msgbox strTemp

set objFile = Nothing
set objFso = Nothing
我的尝试

text1
    1 5 7
hello world 5. This is Samurai 10 not samurai10.
text2
text1
    2 6 8
hello world 6. This is Samurai 11 not samurai10.
text2
const forReading = 1
set objFso = createObject("scripting.filesystemobject")
strFilePath = split(wscript.scriptFullName,wscript.scriptName)(0) & "haha.txt"
set objFile = objFso.openTextFile(strFilePath, forReading)
set objReg = new RegExp
With objReg
    .Global = True
    .ignoreCase = true
    .multiline = true
    .pattern = "\b(\d+)\b"
End With

    strTemp = objFile.readAll()
    strTemp = objReg.replace(strTemp,cint("$1")+1)      '<--Here, I am getting the "Type mismatch 'Cint'" error. I just wanted to increment the number which was captured in Group 1
    msgbox strTemp

set objFile = Nothing
set objFso = Nothing
const forReading=1
设置objFso=createObject(“scripting.filesystemobject”)
strFilePath=split(wscript.scriptFullName,wscript.scriptName)(0)和“haha.txt”
设置objFile=objFso.openTextFile(strFilePath,forReading)
set objReg=new RegExp
带objReg
.Global=True
.ignoreCase=true
.multiline=true
.pattern=“\b(\d+)\b”
以
strTemp=objFile.readAll()
strTemp=objReg.replace(strTemp,cint($1”)+1)为
cint($1”)
,您试图将
$1
字符串转换为
int
,而不是捕获的数字本身

您可以使用以下解决方法:

Dim strTemp As String, val As String
Dim offset As Integer
Dim objReg As regExp

strTemp = "text1" & vbLf & "    1 5 7" & vbLf & "hello world 5. This is Samurai 10 not samurai10." & vbLf & "text2 "

Set objReg = New regExp
With objReg
    .Global = True
    .Pattern = "\b\d+\b"
End With

For Each m In objReg.Execute(strTemp) ' Get all the matches in the original string
    val = CStr(CInt(m.Value) + 1)  ' The incremented matched number value
    strTemp = Left(strTemp, m.FirstIndex + offset) _
        & val _
        & Mid(strTemp, m.FirstIndex + Len(m.Value) + offset + 1) ' Text before match, modified value, the rest
    offset = offset + Len(val) - Len(m.Value) ' Need this to calculate the right index of the next match since when incrementing, the strTemp length may differ from its original length and match indices will mismatch
Next m

当对
strTemp=“ab2cd99def999”
“\d+”
regex使用相同的代码时,会产生预期的
ab3cd100def1000

只需替换代码:

strTemp = objReg.replace(strTemp,cint("$1")+1)

这是我得到的输出:


请使用
cint($1”)
检查函数的引用,您试图将
$1
转换为int,而不是捕获的数字。因此,在将1添加到捕获的数字并用递增的数字替换回文件之前,我应该如何转换捕获的数字?在VBScript中无法做到这一点。您必须找到匹配的位置,并相应地替换为修改后的匹配值。感谢您的解决方案。这对我来说也很有效,而且简单得多。我希望看到你的答案,但不确定这怎么会不被接受。