Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a String using Regular Expressions 1

Status
Not open for further replies.

piscis

Programmer
Jun 26, 2003
29
PR
Gentlemen:


I have this kind of strings coming from the RS232 port into a RichTextBox1. I'm only interested in the numbers right after the equal sign "="

I need to extract this numbers in the same line order shown and place the value into textboxes, each value will go into its own textbox

(there are 110 lines in the final rs232 file). Any idea? I'm using VBNet 2003.

Thanks everyone.

Rs232 file

Sel. 1
Pago = 119
Libre = 0
Test = 0
Desc.= 0
Sel. 2
Pago = 9
Libre = 0
Test = 0
Desc.= 0
Sel. 3
Pago = 322
Libre = 0
Test = 0
Desc.= 0
 
Hi,

I don't understand how you want the values to appear in the textboxed (added? or do you have 100 textboxes?)
Anyway, you can use a regular expression to do what you want - even though it should also be easy to pick out the values without RegEx (all lines containing a '=', the value is all after the '=' trimmed).....
-------------------------------------
Imports System.Text.RegularExpressions
....
Dim MyRegEx = New Regex(&quot;=(?<MyPat>\s+\d+)&quot;)
Dim s As String = &quot;Pago = 119&quot; & vbCrLf & &quot;Libre = 0 &quot; & vbCrLf & &quot;Test = 0 &quot; & vbCrLf & &quot;Desc.= 0 &quot; & vbCrLf & &quot;Sel. 2 &quot; & vbCrLf & &quot;Pago = 9 &quot; & vbCrLf & &quot;Libre = 0 &quot; & vbCrLf & &quot;Test = 0 &quot; & vbCrLf & &quot;Desc.= 0 &quot;
Dim Mc As MatchCollection = MyRegEx.Matches(s)
Dim M As Match
If Mc.Count > 0 Then
For Each M In Mc
MessageBox.Show(M.Groups(&quot;MyPat&quot;).Value)
Next
End If
----------------------------

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
sunaj:

Thank you for your prompt reply.

I already have the 110 textboxes on the form (txtBox1 to txtBox110)

How can I modified this code so the numbers extracted go into these textboxes.

Thanks

Andy
 
With the help of custom24's post at thread796-520369, you could do something like:

------------------------------------------------
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim MyRegEx = New Regex(&quot;=(?<MyPat>\s+\d+)&quot;)
Dim s As String = &quot;Pago = 119&quot; & vbCrLf & &quot;Libre = 0 &quot; & vbCrLf & &quot;Test = 0 &quot;
Dim Mc As MatchCollection = MyRegEx.Matches(s)
Dim M As Match
Dim i As Integer
If Mc.Count > 0 Then
i = 0
For Each M In Mc
i += 1
PutInTextBox(&quot;TextBox&quot; & CStr(i), M.Groups(&quot;MyPat&quot;).Value)
Next
End If
End Sub

Private Sub PutInTextBox(ByVal TBName As String, ByVal msg As String)
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox And ctl.Name = TBName Then
ctl.Text = msg
End If
Next
End Sub
----------------------------------------

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
sunaj:

These are the two errors that I got;

On this line of code;
Dim MyRegEx
I got the following error &quot;Option Strict On requires all Variable Declaration to have an &quot;As&quot; clause&quot;

On this line of code;
MyRegEx.Matches
I got the following error &quot;Option Strict On disallows late binding&quot;

Do you know why?
 
For other reasons I had turned off option strict in my test RegEx project (I usually never do that!). Replacing '=' in the assignment with 'as' should do the trick.

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
sunaj:

I modified this line in your code to; Dim s As String = txtStatus.Text

because in this textbox is were the multiline file resides, then added 9 textboxes, click the button and nothing appeared in the 9 textboxes. I'm doing something wrong?



Imports System.Text.RegularExpressions

Dim MyRegEx As New Regex(&quot;=(?<MyPat>\s+\d+)&quot;)
Dim s As String = txtStatus.Text
Dim Mc As MatchCollection = MyRegEx.Matches(s)
Dim M As Match
Dim i As Integer
If Mc.Count > 0 Then
i = 0
For Each M In Mc
i += 1
PutInTextBox(&quot;TextBox&quot; & CStr(i), M.Groups(&quot;MyPat&quot;).Value)
Next
End If

End Function

Private Sub PutInTextBox(ByVal TBName As String, ByVal msg As String)
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox And ctl.Name = TBName Then
ctl.Text = msg
End If
Next
End Sub
 
You assignment of s is correct (you might want to call it something more logical, the code here is just to let you know how it can be done, you also need to add error handling etc.)
Does the names of the textboxes match the string?
Do you understand what PutInTextBox does?
Have you tried to debug code code by stepping through it while watching the relevant variables?

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top