ManJoey
ManJoey

Reputation: 213

Not able to increment numbers captured in Group 1 of a regex match

I need to increment all the numbers(which are not a part of a word) by 1 in a text file. I tried using the pattern \b(\d+)\b to capture all such numbers but I am not able to increment(add 1 to them) them in the file.

Input

text1
    1 5 7
hello world 5. This is Samurai 10 not samurai10.
text2

Expected Output

text1
    2 6 8
hello world 6. This is Samurai 11 not samurai10.
text2

My attempt

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

If I replace the line strTemp = objReg.replace(strTemp,cint("$1")+1) with strTemp = objReg.replace(strTemp,"_$1_"), I get the following output which means that I am able to get the required numbers which need to be incremented:

enter image description here

I am just not able to increment them. Would really appreciate any help!!!

Upvotes: 3

Views: 134

Answers (2)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Just replace the code:

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

with

set objMatches = objReg.execute(strTemp)
for each match in objMatches
    strTemp = left(strTemp, match.firstIndex) & replace(strTemp, match.value, cInt(match.value)+1, match.firstIndex+1, 1)
next
msgbox strTemp

This is the output I got:

enter image description here

Check the reference for the replace function

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627128

With cint("$1"), you are trying to cast $1 string to int, not the captured number itself.

You may use the following workaround:

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

enter image description here

The same code when used against strTemp = "ab2cd99def999" and "\d+" regex yields the expected ab3cd100def1000.

Upvotes: 3

Related Questions