0

Good morning,

I'm new to VBScript and not great with RegEx just yet. I'm working on a project in which I need to match a pre-existing string to the beginning of a line in a text file, then place that whole line into a string. In my test, I can assign my own string, but in the production environment, it will pull the string from an object int he application. For example, the string would be "0001", and the beginning of the line in the text file would be 0001, followed by the rest of the text that I also need to apply to the new string. Below is the code that I have so far. My issue is that I don't know how to apply the current string to the RegEx pattern, or what else I would need to include in it to perform exactly this search.

Dim strCode
strCode = "0001"

Dim objFSO, objFile, objRegEx
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = strCode & 'This is where I'm not sure exactly how to apply RegEx

Dim afterMid
Dim n
n = 4

Dim result
Dim newString
Dim LogFile
LogFile = "c:\Users\runto\Documents\Test Logfile.txt"
Set objFile = objFSO.OpenTextFile(LogFile,1)

Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)
    If colMatches.Count > 0 Then
    For Each strCode in colMatches
        newString = strSearchString
    Next
    End If
Loop

MsgBox newString

Any help would be massively appreciated. Thanks!

2
  • I've formatted your code to be displayed as code - just start each line with four spaces - but I'm not sure where blank lines were supposed to be. Please correct my guesses! Commented Jun 21, 2018 at 15:32
  • 3
    why not just if left(strSearchString, len(strCode)) = strCode ? Commented Jun 21, 2018 at 15:55

1 Answer 1

2

Match line starting with strCode:

objRegEx.Pattern = "^" & strCode & ".*"

'^'     = Anchor to the start of the string
strCode = followed by your pattern
'.'     = followed by any character
'*'     = followed by zero or more occurrences of the previous character

So the regex becomes "^0001.*"

Oh and you can use

objRegEx.Test(strSearchString)

To see if the string matches your pattern.

Update: Test script illustrates how to first escape non-alphanumeric characters, then performs the comparison:

Dim strCode
Dim strSearchStr
strCode = "0.0[1"
strSearchString = "0.0[1 - test string"

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "([^A-Za-z0-9])"   ' Match non-alphanum chars
objRegEx.global = True
strCode = objRegEx.replace(strCode, "\$1")  ' Escape with a backslash
'msgbox strCode
objRegEx.Pattern = "^" & strCode & ".*"     ' Compare like before
if objRegEx.Test(strSearchString) then
  msgbox "match"
else
  msgbox "No match"
end if
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I will give this a shot as soon as I get a chance and let you know how it works out.
This will not work correctly when strCode contains special characters (e.g. dots or square brackets).
@AnsgarWiechers Good point! I showed how to escape non alph numeric characters. A bit brute force but it seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.