1

I need to find the folder name with special word. My code doesn't work. Would someone tell me how to solve it. I want the folder name with or without () would be fine. I am not sure how many digit in the blanket. There are example:

C:\test\REG33006\2017-03-09
C:\test\REG33006\2017-03-09(1)
C:\test\REG33006\2017-03-09(100)

There is my code in vb:

 Dim Dir as string="C:\test\REG33006\2017-03-09(1)"
 Dim patt As String ="^C:\\test\REG33006\\2017-03-09\(*\d*\)*$"
 Dim regex As Regex = New Regex(patt)
 Dim match As Match = regex.Match(Dir)
     If match.Success Then
         If Dir.Contains(")") Then
             Dim indexStart As Integer = Dir.IndexOf("(")
               maxNumber = CInt(Dir.Substring(indexStart + 1, Dir.Length -  indexStart))


         End If
     End If
user819774
  • 1,456
  • 1
  • 19
  • 48

2 Answers2

1

The parenthesis define a capturing group. You can use sth like

    dim patt as string = "C:\\test\\REG33006\\2017-03-09\((.*)\)"
    if match.success then
         result = match.groups(1).value
Robert L.
  • 137
  • 1
  • 13
0

You may use

 Dim regex As Regex = New Regex("^C:\\test\\REG33006\\2017-03-09(?:\(([0-9]*)\))?$")
 Dim match As Match = regex.Match(Dir)
 If match.Success Then
     maxnumber  = match.Group(1).Value
 End If

The regex matches:

  • ^ - start of a string
  • C:\\test\\REG33006\\2017-03-09 - a literal char sequence C:\test\REG33006\2017-03-09
  • (?:\(([0-9]*)\))? - an optional (1 or 0 times) sequence of:
    • \( - a (
    • ([0-9]*) - Group 1 (its value is retrieved with match.Group(1).Value): zero or more digits
    • \) - a )
  • $ - end of string,
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563