("/gallery/static/newest/\d*/desc/\d*/nosets/19/")
works....
("/gallery/static/newest/\d*/desc/\d*/nosets/" & strAge & "/")
doesn't work....
How can I get my RegEx pattern to work with a variable? Or can't I?
It seems possible, as Karl Kieninger suggested, that you have extra characters in strAge. You can remove those. Developing on sln's idea, you can change the ,s to |s once you have a clean string:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim textToCheck As String = "/gallery/static/newest/123/desc/456/nosets/20/"
Dim strAge As String = "18,19,20,21"
' remove any unwanted chars and change the ","s to "|"s.
strAge = Regex.Replace(strAge, "[^0-9,]", "").Replace(","c, "|"c)
Dim pattern As String = "/gallery/static/newest/\d*/desc/\d*/nosets/(?:" & strAge & ")/"
Console.WriteLine(pattern)
Console.WriteLine(textToCheck & " " & Regex.IsMatch(textToCheck, pattern).ToString)
Console.ReadLine()
End Sub
End Module
Outputs:
/gallery/static/newest/\d*/desc/\d*/nosets/(?:18|19|20|21)/
/gallery/static/newest/123/desc/456/nosets/20/ True
Also, do you really mean \d* (zero or more occurrences) or would \d+ (one or more occurrences) be better?
\d+ would be better to use in this case because \d* means it can also match no digits. Are you saying that you want the URL to contain the commas, or that the commas separate different numbers which could be matches?
strAgeis not guaranteed to be a simple number remember to escape the input see RegEx.Escape