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
3
  • Try with a capturing group - "^C:\\test\REG33006\\2017-03-09(?:\((\d*)\))?$" -> If match.Success Then maxnumber = match.Group(1).Value Commented Mar 9, 2017 at 15:12
  • @WiktorStribiżew thank it is work Commented Mar 9, 2017 at 20:47
  • I added an answer with the pattern explanation. Commented Mar 9, 2017 at 20:51

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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,

1 Comment

Note that \d is different from [0-9] in .NET regex (by default).

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.