none
count 3 digit RRS feed

  • Question

  • Hi all,

    1. I have string to check in List VTxt.

    2. I have string to Find in List V.

    3. every each list in V must join with digit 0 to 9.It became 3 digit.

    my question is how to count that 3 digit using regex or maybe other way in. click button4.

    (count 3 digit every each item List VV  in VTxt

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    Imports System.Windows.Forms
    
    Public Class Form1
    
        Dim VTxt As New List(Of String)
        Dim V As New List(Of String)
        Dim VV As New List(Of String)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim sr As New System.IO.StreamReader("C:\Users\User\Documents\C.txt")
    
            Do While sr.Peek <> -1
                Dim Lines As String = sr.ReadToEnd
                TextBox1.AppendText(Lines & vbCrLf)
                VTxt.Add(Lines)
            Loop
    
        End Sub
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            V.Add("82")
            V.Add("85")
            V.Add("87")
            V.Add("25")
            V.Add("27")
            V.Add("57")
            V.Add("53")
            V.Add("52")
            V.Add("51")
            V.Add("32")
            V.Add("31")
            V.Add("21")
            V.Add("01")
            V.Add("09")
            V.Add("09")
            V.Add("19")
            V.Add("19")
            V.Add("99")
            V.Add("82")
            V.Add("87")
            V.Add("89")
            V.Add("27")
            V.Add("29")
      
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            For Each List2D As String In V
                For I As Integer = 0 To 9
                    Dim Str3 As String = List2D & I
                    TextBox3.AppendText(Str3 & vbCrLf)
                    VV.Add(Str3 & vbCrLf)
                Next
            Next
        End Sub
    
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            For Each StringToCheck As String In VTxt
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
                '2 3 7
                '0 1 2
    
                For Each S As String In StringsToCheck
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2), _
                                                     S(0) & S(2) & S(1), _
                                                     S(1) & S(0) & S(2), _
                                                     S(1) & S(2) & S(0), _
                                                     S(2) & S(1) & S(0), _
                                                     S(2) & S(0) & S(1)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        Dim Find As String = StringsToFind(J)
                        Dim count As Integer = VTxt.SelectMany(Function(d) d.Split(","c)).Sum(Function(n) System.Text.RegularExpressions.Regex.Matches(n, Find(0) & "(?=.*?" & Find(1) & ")|" & Find(1) & "(?=.*?" & Find(0) & ")").Count)
                        TextBox2.AppendText(CStr(count) & "   =>  " & Find(0) & Find(1) & "  or  " & Find(1) & Find(0) & vbCrLf)
                    Next
                Next
            Next
    
        End Sub
    End Class


    • Edited by mipakteh Friday, March 30, 2018 8:52 AM add
    Friday, March 30, 2018 8:26 AM

Answers

  • How to make combination of "pattern" as 196,169 961,916 619,691.

    A regex is a very poor tool for doing permutations.   You would be better off to calculate the permutations separately, then use a regex for a simple match with each one. Since you have only 3 characters in the string then the number of permutations is 6, so it is probably simplest to hard code them.  Something like:

    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(3), _
                         S(1) & S(3) & S(2), _
                         S(2) & S(1) & S(3), _
                         S(2) & S(3) & S(1)}
    

    Then iterate through P, feeding each one into your match regex to get the count.

    • Marked as answer by mipakteh Sunday, April 8, 2018 2:09 AM
    Friday, April 6, 2018 6:37 AM

All replies

  • Please insert a screenshot of your Form or share it via cloud storage (e.g. OneDrive, Dropbox, etc.).

    Ashidacchi -- http://hokusosha.com/

    Friday, March 30, 2018 12:07 PM
  • You still have this question open here:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/29a0f8c0-38b6-44e7-832f-f041faeb6611/string-in-list?forum=vbgeneral#91492ef7-1636-4db5-8170-fbd2095117be

    What happened to the code sample I provided in that thread?  You didn't explain how you came up with the counts you were expecting.  The test data you supplied did not contain the values you were expecting.

    As requested in the other thread, you will need to provide a short example of test data along with all of the expected results from that test data.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Friday, March 30, 2018 12:44 PM
    Moderator
  • How to share with you in dropbox.
    Friday, March 30, 2018 1:34 PM
  • How to share with you in dropbox.

    You've been posting long enough that you should be verified and able to insert images in your thread using the Insert image button in the toolbar of the post editor.

    A good example of a short bit of sample data along with the desired outputs for that sample would be far more useful than a screenshot of the program.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 30, 2018 2:19 PM
    Moderator
  • the latest code you provide very good and not having error, reed Kimble.

    I try test if I change to Regex, It possible or not.In Click button can count only 2 digit not 3 digit with Regex and how to count 3 digit with Regex.

    It simple question how to find items List VV in List VTxt.

    Dim sr As New System.IO.StreamReader("C:\Users\User\Documents\C.txt")

    8257,5321,0199,8279,8661,2120,6299,4000,0752,6975,9809,9406,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201
    6235,5507,0126,9753,0354,0493,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0490,1510,9913
    1954,9691,7223,0899,1992,3609,3259,5081,4204,3116,5806,7052,2347,9917,5773,7416,4605,0541,0548,2134,6627,0172,0617



    • Edited by mipakteh Friday, March 30, 2018 2:53 PM add
    Friday, March 30, 2018 2:48 PM
  • the latest code you provide very good and not having error, reed Kimble.

    I try test if I change to Regex


    I don't see what you would gain by using Regex.  It is a fairly expensive class to use in .Net so I can't imagine that you would gain much in performance over the algorithm provided in the other thread.  If you need to improve the performance of that algorithm, there are other ways to do so.  I designed it as close to your original code as possible for your understanding, it was not designed to be optimized for performance (for example, the string.split is not necessary as you could iterate over the character collection and stick to character-level comparisons, or do everything at the byte level).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 30, 2018 4:36 PM
    Moderator
  • In Click button can count only 2 digit not 3 digit with Regex and how to count 3 digit with Regex.

    What is the regex you are using for 2-digit?   It should be possible to change this from 2-digit to 3-digit.

    Friday, March 30, 2018 9:46 PM
  • In Click button can count only 2 digit not 3 digit with Regex and how to count 3 digit with Regex.

    What is the regex you are using for 2-digit?   It should be possible to change this from 2-digit to 3-digit.


    Its inside the "SelectMany" near the bottom of the code supplied.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 30, 2018 9:52 PM
    Moderator
  • Hi Acamar,

    Dim count As Integer = VTxt.SelectMany(Function(d) d.Split(","c)).Sum(Function(n) System.Text.RegularExpressions.Regex.Matches(n, Find(0) & "(?=.*?" & Find(1) & ")|" & Find(1) & "(?=.*?" & Find(0) & ")").Count)

    Saturday, March 31, 2018 1:55 AM
  •       Dim count As Integer = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(s) System.Text.RegularExpressions.Regex.Matches(s, stringToFind(0) & "(?=.*?" & stringToFind(1) & ")|" & "(?=.*?" & stringToFind(2) & ")|" _
                                                                                                                                            & stringToFind(0) & "(?=.*?" & stringToFind(2) & ")" & "(?=.*?" & stringToFind(1) & ")").Count)
    

    help where to write.

    thank

    Saturday, March 31, 2018 5:04 AM
  • Dim count As Integer = VTxt.SelectMany(Function(d) d.Split(","c)).Sum(Function(n) System.Text.RegularExpressions.Regex.Matches(n, Find(0) & "(?=.*?" & Find(1) & ")|" & Find(1) & "(?=.*?" & Find(0) & ")").Count)

    That's your select statement.  The regex is

       .Matches(n, Find(0) & "(?=.*?" & Find(1) & ")|" & Find(1) & "(?=.*?" & Find(0) & ")")

    You need to use your regex documentation to break that statement down into its components so that you can see what each part of it does.   Then, you will be able to adjust it to move from 2-digit to 3-digit.

    I would strongly recommend that you start by separating that statement out into as many separate statements as you can, such as regex, count, sum and split.  Currently, it is far too complex for you to have any chance of debugging it. 

    Saturday, March 31, 2018 5:37 AM
  •    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs)
    
            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,0906,3557,7494,7052,8155,6382,0469,2491,7664,9214,5918,0544,9261")
    
            'strings contain Find
            '6941
            '6129
            '9261,7691
    
            Dim Find As String = "961"
    
            Dim count4 As Integer = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(s) System.Text.RegularExpressions.Regex.Matches _
            (s, "(?=.*?" & Find(0) & ")|" & "(?=.*?" & Find(1) & ")|" & "(?=.*?" & Find(2) & ")|" & _
            "(?=.*?" & Find(0) & ")" & "(?=.*?" & Find(2) & ")" & "(?=.*?" & Find(1) & ")" & _
            "(?=.*?" & Find(1) & ")|" & "(?=.*?" & Find(2) & ")|" & "(?=.*?" & Find(0) & ")|" & _
            "(?=.*?" & Find(1) & ")" & "(?=.*?" & Find(0) & ")" & "(?=.*?" & Find(2) & ")" & _
            "(?=.*?" & Find(2) & ")|" & "(?=.*?" & Find(1) & ")|" & "(?=.*?" & Find(0) & ")|" & _
            "(?=.*?" & Find(2) & ")" & "(?=.*?" & Find(0) & ")" & "(?=.*?" & Find(1) & ")").Count)
    
            'Result show in TextBox7 after
            '961 = 142
            Me.TextBox7.AppendText(count4.ToString & vbCrLf)
    
            Dim count5 As Integer = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(s) System.Text.RegularExpressions.Regex.Matches _
            (s, Find(0) & Find(1) & "(?=.*?" & Find(2) & ")|" & _
             Find(0) & Find(2) & "(?=.*?" & Find(1) & ")" & _
             Find(1) & Find(2) & "(?=.*?" & Find(0) & ")|" & _
             Find(1) & Find(0) & "(?=.*?" & Find(2) & ")" & _
             Find(2) & Find(1) & "(?=.*?" & Find(0) & ")|" & _
             Find(2) & Find(0) & "(?=.*?" & Find(1) & ")").Count)
    
            'Result show in TextBox8 after 
            '961 = 0
            Me.TextBox8.AppendText(count5.ToString & vbCrLf)
    
            Dim count6 As Integer = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(s) System.Text.RegularExpressions.Regex.Matches _
           (s, Find(0) & "(?=.*?" & Find(1) & ")|" & Find(2) & _
            Find(0) & "(?=.*?" & Find(2) & ")" & Find(1) & _
            Find(1) & "(?=.*?" & Find(2) & ")|" & Find(0) & _
            Find(1) & "(?=.*?" & Find(0) & ")" & Find(2) & _
            Find(2) & "(?=.*?" & Find(1) & ")|" & Find(0) & _
            Find(2) & "(?=.*?" & Find(0) & ")" & Find(1)).Count)
    
            'Result show in TextBox9 after 
            '961 = 5
            Me.TextBox9.AppendText(count6.ToString & vbCrLf)
        End Sub
    I Don't know Acamar..how me some example.
    Tuesday, April 3, 2018 2:24 PM
  •    Dim count4 As Integer = Data.SelectMany(Function(d) d.Split(","c))
    .Sum(Function(s) System.Text.RegularExpressions.Regex.Matches _

    You are still using a code statement that is far too complex.  Your statement includes a regex match, a Sum function, a Split function and a SelectMany function.  There is no way that you will be able to debug that.  You have to know the exact data string that the regex is working with.

    Start with the actual data that you are expecting to feed to the regex - hard coded. Feed it to the regex.  That will produce a collection of Matches. Then use some simple code to see what you get:

     Dim pattern As String = "..."
     Dim rgx As New Regex(pattern)
     Dim sentence As String = "Who writes these notes?"

    For Each match As Match In rgx.Matches( ... ) Me.TextBox7.AppendText(match.Value & " : " & match.Index & vbCrLf) Next

    You could get the pattern from a text box, so that you can re-type it and test it immediately.  Or use one of the on-line regex test utilities, such as https://regexr.com/

    Once you get that working correctly you can add the code to prepare the data from the original text, and format the result, testing it at each step.

    Tuesday, April 3, 2018 9:58 PM
  • How to make combination of "pattern" as 196,169 961,916 619,691.

       Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
            Dim pattern As String = "961"
            Dim rgx As New Regex(pattern)
            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 Counter As Integer = 0
            For Each match As Match In rgx.Matches(str)
                Counter = Counter + 1
                Me.TextBox10.AppendText(match.Value & " : " & match.Index & "  :  " & Counter & vbCrLf)
            Next
        End Sub

    Thursday, April 5, 2018 1:33 PM
  • How to make combination of "pattern" as 196,169 961,916 619,691.

    A regex is a very poor tool for doing permutations.   You would be better off to calculate the permutations separately, then use a regex for a simple match with each one. Since you have only 3 characters in the string then the number of permutations is 6, so it is probably simplest to hard code them.  Something like:

    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(3), _
                         S(1) & S(3) & S(2), _
                         S(2) & S(1) & S(3), _
                         S(2) & S(3) & S(1)}
    

    Then iterate through P, feeding each one into your match regex to get the count.

    • Marked as answer by mipakteh Sunday, April 8, 2018 2:09 AM
    Friday, April 6, 2018 6:37 AM
  •   Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.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 P As String() = str.Split(","c)
    
            For Each S As String In P
                Dim Pattern As String() = {S(0) & S(1) & S(2), _
                                     S(0) & S(2) & S(1), _
                                     S(1) & S(2) & S(3), _
                                     S(1) & S(3) & S(2), _
                                     S(2) & S(1) & S(3), _
                                     S(2) & S(3) & S(1)}
    
                For I As Integer = 0 To Pattern.Count - 1
                    Dim M As String = Pattern(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.TextBox12.AppendText(match.Value & " : " & match.Index & "  :  " & Counter & vbCrLf)
                    Next
                Next
            Next
    
        End Sub
    give comment.I think my code not exactly correct because I want find string S in str string.
    • Edited by mipakteh Friday, April 6, 2018 1:05 PM add
    Friday, April 6, 2018 10:42 AM
  • Acamar,

    Why this code cannot count for number "9261".

    How to do?.

     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

    Saturday, April 7, 2018 6:05 AM
  • Why this code cannot count for number "9261".

    That would depend on M.  But 9621 is different than the other matches, so I presume that M is not set up for that type of match.  What is your matching rule?

    Saturday, April 7, 2018 8:03 AM
  • matching rule;

    961,916,169,196,619,691 but not 9*61 or 96*1

    Saturday, April 7, 2018 8:36 AM
  • matching rule; 961,916,169,196,619,691 but not 9*61 or 96*1

    That is an example, not a rule.  Your rule should be stated in plain language.   If your question is about why the regex you are using is getting you the result you are getting, then you need to ask that at a regex forum.
    Saturday, April 7, 2018 9:27 PM