Dear Experts,

I have one simple question , and forgive me , I know it is simple , but I am very bad in regular expression.

I have a text which represent time interval , eg [1-5] , ie , the start time is at 1 and ends at 5

1- want to check if the format is like this [number-number]. if not this format then this is error
2- extract the start time .. ie 1 in this case ,,,,
3- extract end time .. which is 5
4- if this question is complex.. is it possible to have multiple intervals , ie if i have 2 intervals , eg [1-5][8-10].. is it easy to detect return array of start time (array containts 1 and 8 ) and the end time (array contains 5 adn 10).


4 Years
Discussion Span
Last Post by Reverend Jim

The regular expression


will match any string of the form


where "#" is any sequence of one or more digits. Once you have a match you can do a Split on the "-". For example

Dim s As String = "123-45"
Dim nums() As String = s.Split("-")

nums(0) will be "123" and nums(1) will be "45"

As for multiple intervals, was the example you gave "[1-5][8-10]" an actual string to parse in the exact format? It would be easier to parse if the separate ranges were separated by blanks or commas as in "12-15 23-49 102-119" or "12-15,23-49,102-119"

Edited by Reverend Jim


Dear Jim,

Thanks for your reply. I really appreicate it so much.

I still have the same problem. I want to check the format [4-8] . not #-#.How to accomplish this ?



Here's a bit of code that will parse the string in the pattern you mentioned and put each set of numbers in a list:

        Dim Str As String = "[1-5][8-10][10-20][21-31]"
        Dim StartTime As New List(Of Integer)
        Dim EndTime As New List(Of Integer)
        Dim ST, ET As String
        Dim TmpStr As String = ""
        While Str.Length > 0
            TmpStr = Str.Substring(1, Str.IndexOf("]") - 1)
            ST = Strings.Left(TmpStr, TmpStr.IndexOf("-"))
            For Each Digit As Char In ST
                If Not Char.IsDigit(Digit) Then
                    MessageBox.Show("String is in the wrong format")
                    Exit While
                End If
            ET = Strings.Right(TmpStr, TmpStr.Length - TmpStr.IndexOf("-") - 1)
            For Each Digit As Char In ET
                If Not Char.IsDigit(Digit) Then
                    MessageBox.Show("String is in the wrong format")
                    Exit While
                End If
            Str = Str.Substring(Str.IndexOf("]") + 1)
        End While

For a collection like this that has an unknown number of elements, I find a List much easier to work with. For format checking I checked each number before it goes into the list. If any digit isn't a number then it exits after a messagebox gives a warning. It's self contained, so that you can put it into a separate sub just pass the string to parse as a parameter and make the 2 lists global.


Try the following (requires Imports System.Text.RegularExpressions)

Dim s As String = "[1-5][8-10][10-20][21-31]"
Dim rex As New Regex("\[\d+-\d+\]")

For Each m As Match In rex.Matches(s)
    Dim range As String = m.Value.Substring(1, m.Length - 2)
    Dim nums() As String = range.Split("-")
    Debug.WriteLine(range & " = " & nums(0) & " to " & nums(1))

The output is

1-5 = 1 to 5
8-10 = 8 to 10
10-20 = 10 to 20
21-31 = 21 to 31

This will work even if you have spaces or other delimiters embedded such as

[1-5] [8-10] [10-20]   [21-31]

Edited by Reverend Jim

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.