[RESOLVED] how to use regex or an array to display some output from an arithmetic input?
Hi guys!! :wave: I've to make a program that reads an arithmetical sentence from a textbox, e.g 12.3*34+4*5-3 and then on another textbox identify and display what is the content of the first textbox, for example taking the last ex:
If I have this input:
12.3*34+4*5-3
The output should look like this:
<number>12.3
<operator> *
<number> 34
<operator> +
<number> 4
<operator> *
<number> 5
<operator> -
<number> 3
This program should only read this operators like (+,-,*,/).
I'm have in mind 2 ways of doing it, one with regex and the other taking the first sentence, put it on an array, then trim it, and somehow get the values of the array and identify if it is a number or a decimal number and if it's an operator and then display it.
The difficult part or the part that i can't conceptualize :confused: is how to implement it with regex (it should be easier with a regex format than storing it on a array).
I write a bit of code, :D I used an array to store and then to print the numbers and I used the split function to separate the math operator, but I dunno how to print the operators and the tags.
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Sentencia As String = TextBox1.Text
Dim i As Integer
Dim arryTextBox() As String
TextBox2.Clear()
arryTextBox = Sentencia.Split("+", "-", "*", "/")
For i = 0 To UBound(arryTextBox)
Dim Array As String = arryTextBox(i)
'MsgBox(arryTextBox(i)) 'this msgbox works it prints the values of the array
TextBox2.Text = TextBox2.Text & Array & vbCrLf
Next i
End Sub
Btw, I also have the code to validate the input on the textbox ::wink::
Any help will be very appreciated,
Thanks in advanced!!
Re: how to use regex or an array to display some output from an arithmetic input?
vb.net Code:
If Me.txtEquation.Text <> String.Empty Then
Me.txtSeperated.Text = "<number> "
For Each EquChar In Me.txtEquation.Text
Dim OperatorMatch As Match = Regex.Match(EquChar, "(\+|-|\*|/)")
If OperatorMatch.Success Then
Me.txtSeperated.Text &= vbCrLf & "<operator> "
End If
Me.txtSeperated.Text &= EquChar
If OperatorMatch.Success Then
Me.txtSeperated.Text &= vbCrLf & "<number> "
End If
Next
End If
Put this in your button click event; this probably isn't the best way to solve your problem, but it works.
Also, you shouldn't use "Array" as a variable name as it is also a Class and doing so could produce errors.
Re: how to use regex or an array to display some output from an arithmetic input?
OMG WOW!! Thank so much man!! I'll have to study a lot about regex functions. You made my day!!
Thanks for ur advice, i wont write that again ^^
A+++++
Thanks again!!