Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have been trying to figure out why I only get the first string returned to my label using string reader but then nothing after that.

I am using regular expressions to grab newly added data from a string inside a richtextbox to a label for testing purposes but for some reason it only grabs the first match and displays it in the label but any other match after that it does not change the label to the next match. I think it only reads the first match because the condition has been met or it is only reading the first string?


VB
Dim Data4 As New Regex("\b\w{4}\b", RegexOptions.IgnoreCase)
Dim reader As New StringReader(RichTextBox2.Text)
Dim tmp As String = reader.ReadToEnd
Dim MD0 As Match = Data4.Match(tmp)
For Each M As String In RichTextBox2.Lines
           If M.Contains(MD.ToString) Then
               Label4.Text = MD0.ToString
           End If
       Next M



Im new to using string reader
not sure of the ins and outs yet
Posted
Updated 31-Jul-13 9:16am
v2
Comments
ledtech3 31-Jul-13 16:33pm    
Not sure what your trying to match there but you might try looking at Matches instead of Match.
http://msdn.microsoft.com/en-us/library/e7sf90t3.aspx
Kschuler 31-Jul-13 16:55pm    
Good call. You should post this as a solution.
ledtech3 31-Jul-13 17:03pm    
I havn't gotten comfortable posting things as a solution.
But with code samples you have to or it dosen't look right.
Sergey Alexandrovich Kryukov 31-Jul-13 17:28pm    
The problem is different. The idea to do reader.ReadToEnd for the StringReader is no more than gibberish. Please see my answer.
—SA
Kschuler 31-Jul-13 17:33pm    
Yes. You've told the OP that his code is crap. But you didn't tell him how to actually fix his problem and get the result he's looking for. Ledtech3's comment is productive.

Ok after some testing it appears that what it is looking for is words containing 4 Chars.

Here is a small sample that rewrites most of the code used, using the above link as a guide and using a rich text box for input and a standard texbox for output here is what I ended up with.

Input is this.
this is a tesssssst for to mannnnnnny leters tttt AbCd

Output is this.
this
tttt
AbCd


Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    Try
        Dim strbldr As New StringBuilder
        Dim regtext As String
        If tbInput.Text = Nothing Then
            MsgBox("Nothing was entered")
            Exit Sub
        Else
            regtext = tbInput.Text
        End If

        Dim pattern As String = ("\b\w{4}\b")
        Dim rgx As New Regex(pattern)
        For Each M As Match In rgx.Matches(regtext)
            strbldr.AppendLine(M.ToString)
        Next M

        tbOutput.Text = strbldr.ToString
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
 
Share this answer
 
First of all, you do read all text. By some weird reason, you just cannot see it.

The problem is: it makes no sense at all. You see,
VB
Dim reader As New StringReader(RichTextBox2.Text)
Dim tmp As String = reader.ReadToEnd
is equivalent to
VB
Dim tmp As String = RichTextBox2.Text
So, why using StringReader at all? Just don't do it.

—SA
 
Share this answer
 
Comments
Draco2013 1-Aug-13 1:29am    
thank you all for your input I have figured this out with your help!
Sergey Alexandrovich Kryukov 1-Aug-13 8:14am    
Great. You are very welcome.
Good luck, call again.
—SA
ledtech3 1-Aug-13 13:21pm    
Great,glad to hear it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900