none
Regex Reference RRS feed

  • Question

  •  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
            '                    9614                     0196      7691      9610                                                             9261
    
            Dim S As String = "961"
    
            Dim P As String() = {S(0) & S(1) & S(2), _
                                 S(0) & S(2) & S(1), _
                                 S(1) & S(2) & S(0), _
                                 S(1) & S(0) & S(2), _
                                 S(2) & S(1) & S(0), _
                                 S(2) & S(0) & S(1)}
    
            For I As Integer = 0 To P.Count - 1
                Dim M As String = P(I)
                Dim rgx As New Regex(M)
    
                Dim Counter As Integer = 0
                For Each match As Match In rgx.Matches(str)
                    Counter = Counter + 1
                    Me.TextBox1.AppendText(match.Value & " : " & match.Index & "  :  " & Counter & vbCrLf)
                Next
    
            Next
    
        End Sub

    What I try to do;

    Count string "S" in  "str" for 3 digits .All digits count but not for 9261 because the mathcing not follow that rule.

    My question is How to put Regex reference between 9 and 6, 6 and 1.(9*61,96*1,1*69,16*9,6*19,61*9)

    Thank.

    Sunday, April 8, 2018 2:25 AM

Answers

  • May This Help You

    You can achieve same output by another approach Including 9261 & (9*61,96*1,1*69,16*9,6*19,61*9)

     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
            '                    9614                     0196      7691      9610                                                             9261
    
            Dim S As String = "961"
    
            Dim str1 = str.Split(",")
            Dim Counter = 0
    
            For i = 0 To str1.Length - 1
                Dim str2 = str1(i)
                If str2.Contains(S(0)) And str2.Contains(S(1)) And str2.Contains(S(2)) Then
                    Counter += 1
                    TextBox1.AppendText(str2 & " : " & i & "  :  " & Counter & vbCrLf)
                End If
            Next
    
        End Sub

    You can achieve same output by another approach Excluding 9261 & (9*61,96*1,1*69,16*9,6*19,61*9)

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
            '                    9614                     0196      7691      9610                                                             9261
    
            Dim S As String = "961"
            Dim comb As New List(Of String)
    
    
            comb.Add(S(0) + S(1) + S(2))
            comb.Add(S(0) + S(2) + S(1))
            comb.Add(S(1) + S(2) + S(0))
            comb.Add(S(1) + S(0) + S(2))
            comb.Add(S(2) + S(1) + S(0))
            comb.Add(S(2) + S(0) + S(1))
    
            Dim str1 = str.Split(",")
            Dim Counter = 0
    
    
            For i = 0 To str1.Length - 1
                Dim str2 = str1(i)
                For Each item In comb
                    If str2.Contains(item) Then
                        Counter += 1
                        TextBox1.AppendText(str2 & " : " & i & "  :  " & Counter & vbCrLf)
                    End If
                Next
            Next
        End Sub

    Best Regards
    Vikram Manjare
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    • Edited by Vikram Manjare Sunday, April 8, 2018 6:52 AM
    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Sunday, April 8, 2018 4:03 AM
  • Check the next example too:

    Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
    
    Dim S As String = "961"
    Dim p As String = String.Concat(S.Select(Function(c) $"(?=\d*{c})"))
    p = $"(?<=^|,){p}\d\d\d\d(?=,|$)"
    
    Console.WriteLine("Pattern: {0}", p)
    
    For Each m As Match In Regex.Matches(str, p)
       Console.WriteLine("Found: {0} at {1}", m.Value, m.Index)
    Next

    For convenience, it is a Console program for experimenting.

    The string S can have a length between 1 and 4.







    • Edited by Viorel_MVP Sunday, April 8, 2018 8:54 AM
    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Sunday, April 8, 2018 8:42 AM
  • Hi Vikram,

    I test for 3 line data and result show not correct.

         Dim Data As New List(Of String)
    
    
            Data.Add("9600,4154,2972,5595,7086,2947,5915,7463,2233,3897,3955,8868,9819,0025,6330,8558,6750,7855,6941,7531,0603,5496,1785")
            Data.Add("4627,5223,9959,6243,1878,5062,9679,1898,4857,1031,5957,6027,2207,1565,9258,2577,2890,1703,1813,6062,3387,4583,6129")
            Data.Add("5254,4487,3650,8151,7743,0195,2452,7691,4643,5990,1990,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261")
    
            For Each Str As String In Data
    
                'Str = Str.Replace(vbCrLf, ",")
    
                Dim str1 As String() = Str.ToString.Split(","c) ' ****
                Dim S As String = "192"
    
                Dim Counter As Integer = 0
    
                For i As Integer = 0 To str1.Length - 1
                    Dim str2 As String = str1(i)
                    If str2.Contains(S(0)) And str2.Contains(S(1)) And str2.Contains(S(2)) Then
                        Counter += 1
                        TextBox3.AppendText(str2 & " : " & S & " : " & i & "  :  " & Counter & vbCrLf)
                    End If
                Next
            Next


    Result show

    6129 : 192 : 22  :  1
    2491 : 192 : 17  :  1
    9214 : 192 : 19  :  2
    9261 : 192 : 22  :  3

    Expected Result is

    6129 : 192 : 45  :  1
    2491 : 192 : 63  :  2
    9214 : 192 : 65  :  3
    9261 : 192 : 68  :  4


            Dim Data As New List(Of String)
    
    
            Data.Add("9600,4154,2972,5595,7086,2947,5915,7463,2233,3897,3955,8868,9819,0025,6330,8558,6750,7855,6941,7531,0603,5496,1785")
            Data.Add("4627,5223,9959,6243,1878,5062,9679,1898,4857,1031,5957,6027,2207,1565,9258,2577,2890,1703,1813,6062,3387,4583,6129")
            Data.Add("5254,4487,3650,8151,7743,0195,2452,7691,4643,5990,1990,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261")
    
            Dim S As String = "192"
            Dim Counter As Integer = 0
            Dim offset As Integer = 0
            For Each Str1 In Data
                Dim str2 As String() = Str1.Split(","c)
                For i As Integer = 0 To str2.Length - 1
                    Dim str3 As String = str2(i)
                    If str3.Contains(S(0)) And str3.Contains(S(1)) And str3.Contains(S(2)) Then
                        Counter += 1
                        TextBox3.AppendText(str3 & " : " & S & " : " & i + offset & "  :  " & Counter & vbCrLf)
                    End If
                Next
                offset += str2.Length
            Next
    

    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Thursday, April 19, 2018 4:48 PM
  • It appears that you do not have yet the modern VB (Visual Studio 2017).

    Then try the adjusted variant:

    Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
    
    Dim S As String = "961"
    Dim p As String = String.Concat(S.Select(Function(c) "(?=\d*" & c & ")"))
    p = "(?<=^|,)" & p & "\d\d\d\d(?=,|$)"
    
    Console.WriteLine("Pattern: {0}", p)
    
    For Each m As Match In Regex.Matches(str, p)
       Console.WriteLine("Found: {0} at {1}", m.Value, m.Index)
    Next
    

    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Thursday, April 19, 2018 6:43 PM

All replies

  • May This Help You

    You can achieve same output by another approach Including 9261 & (9*61,96*1,1*69,16*9,6*19,61*9)

     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
            '                    9614                     0196      7691      9610                                                             9261
    
            Dim S As String = "961"
    
            Dim str1 = str.Split(",")
            Dim Counter = 0
    
            For i = 0 To str1.Length - 1
                Dim str2 = str1(i)
                If str2.Contains(S(0)) And str2.Contains(S(1)) And str2.Contains(S(2)) Then
                    Counter += 1
                    TextBox1.AppendText(str2 & " : " & i & "  :  " & Counter & vbCrLf)
                End If
            Next
    
        End Sub

    You can achieve same output by another approach Excluding 9261 & (9*61,96*1,1*69,16*9,6*19,61*9)

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
            '                    9614                     0196      7691      9610                                                             9261
    
            Dim S As String = "961"
            Dim comb As New List(Of String)
    
    
            comb.Add(S(0) + S(1) + S(2))
            comb.Add(S(0) + S(2) + S(1))
            comb.Add(S(1) + S(2) + S(0))
            comb.Add(S(1) + S(0) + S(2))
            comb.Add(S(2) + S(1) + S(0))
            comb.Add(S(2) + S(0) + S(1))
    
            Dim str1 = str.Split(",")
            Dim Counter = 0
    
    
            For i = 0 To str1.Length - 1
                Dim str2 = str1(i)
                For Each item In comb
                    If str2.Contains(item) Then
                        Counter += 1
                        TextBox1.AppendText(str2 & " : " & i & "  :  " & Counter & vbCrLf)
                    End If
                Next
            Next
        End Sub

    Best Regards
    Vikram Manjare
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    • Edited by Vikram Manjare Sunday, April 8, 2018 6:52 AM
    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Sunday, April 8, 2018 4:03 AM
  • Check the next example too:

    Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
    
    Dim S As String = "961"
    Dim p As String = String.Concat(S.Select(Function(c) $"(?=\d*{c})"))
    p = $"(?<=^|,){p}\d\d\d\d(?=,|$)"
    
    Console.WriteLine("Pattern: {0}", p)
    
    For Each m As Match In Regex.Matches(str, p)
       Console.WriteLine("Found: {0} at {1}", m.Value, m.Index)
    Next

    For convenience, it is a Console program for experimenting.

    The string S can have a length between 1 and 4.







    • Edited by Viorel_MVP Sunday, April 8, 2018 8:54 AM
    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Sunday, April 8, 2018 8:42 AM
  • Hi Viorel,

    I got error list before debug.

    Error 1 Character is not valid. C:\Users\family\documents\visual studio 2010\Projects\Method Count 3 Digit\Method Count 3 Digit\Form1.vb  $ 151 60 Method Count 3 Digit
    Error 2 'p' is not declared. It may be inaccessible due to its protection level. C:\Users\family\documents\visual studio 2010\Projects\Method Count 3 Digit\Method Count 3 Digit\Form1.vb 153 43 Method Count 3 Digit
    Error 3 'p' is not declared. It may be inaccessible due to its protection level. C:\Users\family\documents\visual studio 2010\Projects\Method Count 3 Digit\Method Count 3 Digit\Form1.vb 155 51 Method Count 3 Digit
        Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
            Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
    
            Dim S As String = "961"
    
          Dim p As String = String.Concat(S.Select(Function(c) $"(?=\d*{c})"))p = $"(?<=^|,){p}\d\d\d\d(?=,|$)"
    
            Console.WriteLine("Pattern: {0}", p)
    
            For Each m As Match In Regex.Matches(str, p)
                Console.WriteLine("Found: {0} at {1}", m.Value, m.Index)
            Next
        End Sub


    • Edited by mipakteh Thursday, April 19, 2018 2:44 PM add
    Thursday, April 19, 2018 2:40 PM
  • Hi Vikram,

    I test for 3 line data and result show not correct.

         Dim Data As New List(Of String)
    
    
            Data.Add("9600,4154,2972,5595,7086,2947,5915,7463,2233,3897,3955,8868,9819,0025,6330,8558,6750,7855,6941,7531,0603,5496,1785")
            Data.Add("4627,5223,9959,6243,1878,5062,9679,1898,4857,1031,5957,6027,2207,1565,9258,2577,2890,1703,1813,6062,3387,4583,6129")
            Data.Add("5254,4487,3650,8151,7743,0195,2452,7691,4643,5990,1990,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261")
    
            For Each Str As String In Data
    
                'Str = Str.Replace(vbCrLf, ",")
    
                Dim str1 As String() = Str.ToString.Split(","c) ' ****
                Dim S As String = "192"
    
                Dim Counter As Integer = 0
    
                For i As Integer = 0 To str1.Length - 1
                    Dim str2 As String = str1(i)
                    If str2.Contains(S(0)) And str2.Contains(S(1)) And str2.Contains(S(2)) Then
                        Counter += 1
                        TextBox3.AppendText(str2 & " : " & S & " : " & i & "  :  " & Counter & vbCrLf)
                    End If
                Next
            Next


    Result show

    6129 : 192 : 22  :  1
    2491 : 192 : 17  :  1
    9214 : 192 : 19  :  2
    9261 : 192 : 22  :  3

    Expected Result is

    6129 : 192 : 45  :  1
    2491 : 192 : 63  :  2
    9214 : 192 : 65  :  3
    9261 : 192 : 68  :  4

    Thursday, April 19, 2018 2:55 PM
  • Hi Vikram,

    I test for 3 line data and result show not correct.

         Dim Data As New List(Of String)
    
    
            Data.Add("9600,4154,2972,5595,7086,2947,5915,7463,2233,3897,3955,8868,9819,0025,6330,8558,6750,7855,6941,7531,0603,5496,1785")
            Data.Add("4627,5223,9959,6243,1878,5062,9679,1898,4857,1031,5957,6027,2207,1565,9258,2577,2890,1703,1813,6062,3387,4583,6129")
            Data.Add("5254,4487,3650,8151,7743,0195,2452,7691,4643,5990,1990,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261")
    
            For Each Str As String In Data
    
                'Str = Str.Replace(vbCrLf, ",")
    
                Dim str1 As String() = Str.ToString.Split(","c) ' ****
                Dim S As String = "192"
    
                Dim Counter As Integer = 0
    
                For i As Integer = 0 To str1.Length - 1
                    Dim str2 As String = str1(i)
                    If str2.Contains(S(0)) And str2.Contains(S(1)) And str2.Contains(S(2)) Then
                        Counter += 1
                        TextBox3.AppendText(str2 & " : " & S & " : " & i & "  :  " & Counter & vbCrLf)
                    End If
                Next
            Next


    Result show

    6129 : 192 : 22  :  1
    2491 : 192 : 17  :  1
    9214 : 192 : 19  :  2
    9261 : 192 : 22  :  3

    Expected Result is

    6129 : 192 : 45  :  1
    2491 : 192 : 63  :  2
    9214 : 192 : 65  :  3
    9261 : 192 : 68  :  4


            Dim Data As New List(Of String)
    
    
            Data.Add("9600,4154,2972,5595,7086,2947,5915,7463,2233,3897,3955,8868,9819,0025,6330,8558,6750,7855,6941,7531,0603,5496,1785")
            Data.Add("4627,5223,9959,6243,1878,5062,9679,1898,4857,1031,5957,6027,2207,1565,9258,2577,2890,1703,1813,6062,3387,4583,6129")
            Data.Add("5254,4487,3650,8151,7743,0195,2452,7691,4643,5990,1990,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261")
    
            Dim S As String = "192"
            Dim Counter As Integer = 0
            Dim offset As Integer = 0
            For Each Str1 In Data
                Dim str2 As String() = Str1.Split(","c)
                For i As Integer = 0 To str2.Length - 1
                    Dim str3 As String = str2(i)
                    If str3.Contains(S(0)) And str3.Contains(S(1)) And str3.Contains(S(2)) Then
                        Counter += 1
                        TextBox3.AppendText(str3 & " : " & S & " : " & i + offset & "  :  " & Counter & vbCrLf)
                    End If
                Next
                offset += str2.Length
            Next
    

    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Thursday, April 19, 2018 4:48 PM
  • It appears that you do not have yet the modern VB (Visual Studio 2017).

    Then try the adjusted variant:

    Dim str As String = "9614,4487,3650,8151,7743,0196,2452,7691,4643,9610,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261"
    
    Dim S As String = "961"
    Dim p As String = String.Concat(S.Select(Function(c) "(?=\d*" & c & ")"))
    p = "(?<=^|,)" & p & "\d\d\d\d(?=,|$)"
    
    Console.WriteLine("Pattern: {0}", p)
    
    For Each m As Match In Regex.Matches(str, p)
       Console.WriteLine("Found: {0} at {1}", m.Value, m.Index)
    Next
    

    • Marked as answer by mipakteh Thursday, May 3, 2018 11:17 AM
    Thursday, April 19, 2018 6:43 PM