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?

Chethan N
  • 1,110
  • 1
  • 9
  • 23
  • 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. – Karl Kieninger Feb 16 '14 at 16:11
  • If the value of ```strAge``` is not guaranteed to be a simple number remember to escape the input see [RegEx.Escape](http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape(v=vs.110).aspx) – Dean Taylor Feb 16 '14 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. – user3316318 Feb 16 '14 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. – user3316318 Feb 16 '14 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. – user3316318 Feb 16 '14 at 17:01
  • If you want to be sure strAge contains a number, use Integer.Parse – MrPaulch Feb 16 '14 at 17:17
  • The problem is that strAge could be 18,19,20,21 in that format. That's how the url expects it. If it was just one number I'd convert it to an integer but it has to be a string because it could contains multiple numbers with commas seperating the ages. In the end, a string variable doesn't work with a RegEx pattern but a lone integer does. My only option is to use RegEx.Split and split the string of numbers unless someone has a better option. Thank you all for all your input on this. – user3316318 Feb 16 '14 at 17:25
  • Split the strAge on comma, then join together to form the regex substring. Something like `"(?:"+ (join(element,"|") ary) + ")"` . Sorry, don't know vb. –  Feb 16 '14 at 18:40

1 Answers1

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?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • 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 Feb 16 '14 at 23:36
  • @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? – Andrew Morton Feb 17 '14 at 16:54