Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include variable in Regular expression pattern

Tags:

regex

vba

I am working on a vba macro which uses regular expression to search for a string pattern in another string.

Regex pattern includes a string (APR24 in code below) which varies. I need to know how to include a variable in the pattern.Could any one please help.

My code is as below

Public Function Regexsrch(ByVal str2bsrchd As String, ByVal str2srch As String) As Boolean
Dim Regex As New VBScript_RegExp_55.RegExp
Dim matches, s
Regex.Pattern = "(\.|\s)APR24(,|\s|\()"
Regex.IgnoreCase = True
    If Regex.Test(str2bsrchd) Then
         Regexsrch = True
Else
        Regexsrch = False
End If
End Function
like image 468
user2009245 Avatar asked Sep 18 '25 18:09

user2009245


1 Answers

So str2srch is "APR24" or some variation? If that is the case you just use concatenation to build up your pattern string.

Public Function Regexsrch(ByVal str2bsrchd As String, ByVal str2srch As String) As Boolean
Dim Regex As New VBScript_RegExp_55.RegExp
Dim matches, s
Regex.Pattern = "(\.|\s)" + str2srch + "(,|\s|\()"
Regex.IgnoreCase = True
    If Regex.Test(str2bsrchd) Then
         Regexsrch = True
Else
        Regexsrch = False
End If
End Function
like image 92
Erik Nedwidek Avatar answered Sep 21 '25 08:09

Erik Nedwidek