1

("/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?

8
  • You should be able to do this. Are you sure the strAge is String that is "19" in this example? Can you debug print the concatenated string? Alternatively use an intermediate variable and debug step-wise. Commented Feb 16, 2014 at 16:11
  • If the value of strAge is not guaranteed to be a simple number remember to escape the input see RegEx.Escape Commented Feb 16, 2014 at 16:20
  • Yeah I tested it out and doesn't work. I was hoping for some other alternative. From my experience you can't break up a pattern in to pieces with variables. Commented Feb 16, 2014 at 16:21
  • Dean, strAge is a number only. I could use Cint and turn into a an integer but it fails regardless. I'll look into RegEx.Escape. Thanks. Commented Feb 16, 2014 at 16:24
  • The & and + operators don't work with it either. I did notice that if I convert strAge to an integer using Cint it did work. The problem now is that their isn't only one number. It could be 18,19. That's why I had it as a string. I could use RegEx.Split but that just makes it more confusing. I mines well hard code all the patterns. Commented Feb 16, 2014 at 17:01

1 Answer 1

0

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I use \d* because the number could be 10 or 97395. They're random numbers. Also, strAge could be 20 with no extra characters but it still doesn't work. Only when I hard code it. I've already used strAge as 18,19,20 because that's what the url wants it to be formatted like. If I hard code it it works. But as a variable it doesn't. Thanks for your solution.
@user3316318 It looks like \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?

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.