none
Count with Reqex RRS feed

  • Question

  • Hi All,

    I have Digits to count but the give the different result.

    1. This result give 5.

     Private Sub Button4_Click_1(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            Dim Data As New List(Of String)
    
            Data.Add("8345,7509,3753,4679")
            Data.Add("8294,9323,8523,7538")
    
            Dim stringToFind As String = "53"
            Dim count As Integer = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(s) Regex.Matches(s, stringToFind(0) & "(?=.*?" & stringToFind(1) & ")|" & stringToFind(1) & "(?=.*?" & stringToFind(0) & ")").Count)
            TextBox3.AppendText(CStr(count) & vbCrLf)
    
        End Sub

    2. This result give 4

    Private Sub Button4_Click_1(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            Dim Data As New List(Of String)
    
            Data.Add("8345,7509,7533,4679")
            Data.Add("8294,9323,8523,7538")
    
            Dim stringToFind As String = "53"
            Dim count As Integer = Data.SelectMany(Function(d) d.Split(","c)).Sum(Function(s) Regex.Matches(s, stringToFind(0) & "(?=.*?" & stringToFind(1) & ")|" & stringToFind(1) & "(?=.*?" & stringToFind(0) & ")").Count)
            TextBox3.AppendText(CStr(count) & vbCrLf)
    
        End Sub

    My question is why the result is different 5 and 4.

    I expected the correct result is 5.

    Saturday, October 7, 2017 3:59 AM

All replies

  • My question is why the result is different 5 and 4.

    Different data will give different results.  You should explain why you believe the two results should be the same.

    Saturday, October 7, 2017 6:44 AM
  • That Data different at 3753 and 7533.But the code count 2 digit match from 4 not count a value.

    That I'm thinking logic.That why that code must give the same result.I hope Acamar can show me a correct 

    code or some example in code .

    Thank Acamar.

    Sunday, October 8, 2017 2:13 AM
  • That Data different at 3753 and 7533.But the code count 2 digit match from 4 not count a value.

    That code will only give the same result if the 'count' for 3753 and 7533 is the same. 

    Presumably, you are expecting 3753  to match at 3753 and 3753, and 7533 to match at 7533 and 7533.  But the regular expression you are using won't do that.   If you break that code down into suitable separate statements instead of one large single impenetrable expression then you have a chance of debugging it to find out why.

    Sunday, October 8, 2017 2:42 AM
  • Hi mipakteh,

    According to the code that you provided, I guess that It will count one time when detect 7533 by your regex, because 7533 and 7533 will only detect 53, 3753 can detect 35 and 53, it get the different result.

    Best Regards,

    Cherry


    MSDN Community Support
    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. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 9, 2017 6:26 AM
    Moderator
  • Help with code..I don't to do it.
    Tuesday, October 10, 2017 11:35 AM
  • Help me with code ,I don't how to do it.
    Tuesday, October 10, 2017 11:36 AM
  • Help with code..I don't to do it.

    How would you do it if you were using pencil and paper?    You can't write code if you don't know what you need to do.  For example - to count '53' or '35' in '7533':

    Is the first digit ('7') a '5' or a '3'?  No. move onto the second digit.

    Is the second digit ('5') a '5' or '3'?  Yes - it's a '5'. So we need to check for '53'.
      Is the first digit following it a '3'?  Yes.  That's '53' - add 1 to the count
      Is the next digit following it a '3'?  Yes. That another '53'.  Add 1 to the count.
      Are there any more following digits left? No - move onto the third digit.

    Is the third digit ('3') a '5' or a '3'?  Yes - it's a '3'. So we need to check for '35'.
      ... and so on.

    When you have written it out like that you can code it.

    Please refer to your post at
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/181429f7-92a2-4df1-b9eb-c7dc0f867707/flexible-count-2-digits?forum=vbgeneral#80d46031-443d-467a-8642-878caebbb274

    because you have not replied to the questions that were asked there.

    Tuesday, October 10, 2017 11:46 AM
  • like this?

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim Input As String = "5337"
    
            Dim Output As String = ""
    
            For i As Integer = 0 To 1
    
                Output = (Input(i) + Input(i + 1)).ToString & vbCrLf & (Input(i) + Input(i + 2)).ToString & vbCrLf & (Input(i + 1) + Input(i)).ToString & vbCrLf & (Input(i + 2) + Input(i)).ToString
    
                TextBox1.AppendText(Output)
                TextBox1.AppendText(vbCrLf)
    
            Next
    
    
        End Sub

    Tuesday, October 10, 2017 12:30 PM
  • like this?

    When you broke your expression down into separate lines of code so that you could debug it, what was the actual value of the regex expression for a string to find of "53"?

    Then describe, in plain language, what that regex expression does.  That will tell you why your results were different for the two different test values.

    Tuesday, October 10, 2017 8:34 PM