Click here to Skip to main content
14,870,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts !
i am making a case in which user can create custom formula by selecting multiple strings,

Now, when user saves the formula, it is in this form

Quote:
(AL01-RM)+(AL03-RM)+20.0


inside brackets Strings are some ids of the elements (that are in datagridview column),
now the scenario is,

1- it gets the above string,
2- find string inside brackets () in datagridview column by Regex pattern "(?<=\().*?(?=\))")
3- if string insides brackets() matches with datagridview column 1 value,
4- now get the value from datagridview column 2 (that is the value of string)
5- now replace value in first input string inside brackets() , and here is i have stuck,
i want string like this
"(10)+(30)+20"

but i get string like this
"(10 )+(AL03-RM)+20.0(AL01-RM)+(30)+20.0"

i have tried different things, but can't get out put like
"(10)+(30)+20"
Please see image for details

Imgur: Image Link for Details[^]

Please help me to solve this.

What I have tried:

Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles SimpleButton2.Click

For i As Integer = 0 To dgv.Rows.Count - 1

'Dim paren As New Regex("\([^)]*\)")
'Dim paren As New Regex("\((.*?)\)")
Dim paren As New Regex("(?<=\().*?(?=\))")
Dim command As String = "((AL01-RM)+(AL03-RM))+20.0"

Dim matches As MatchCollection
matches = paren.Matches(command)

Dim m As Match
For Each m In matches

If m.ToString = dgv.Rows(i).Cells(1).Value Then
RTB2.AppendText(Regex.Replace(command, m.ToString, dgv.Rows(i).Cells(2).Value.ToString))

End If
Next

Next

End Sub
Posted
Updated 21-Jun-17 22:09pm
v2

First of all try to make your mind about the language: it is C# or VB, not both, from the code, it is VB.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   
Comments
Member 10058701 22-Jun-17 6:09am
   
Thanks for your valuable Post, i will go for this.
Something like this:
VB
Dim paren As String = "\([^)]*\)"
Dim command As String = "(AL01-RM)+(AL03-RM)+20.0"
Dim matches As MatchCollection = Regex.Matches(command, paren)
		
For Each m In matches
Dim temp As String = "match = " + m.ToString()
Console.WriteLine(temp)
command = command.Replace(m.ToString(), "(xx)")
Next
	
Console.WriteLine(command)
   
Comments
Member 10058701 22-Jun-17 6:14am
   
Thanks for your Valuable answer, finally is has solved with minor changes with your idea:

Dim paren As String = "(?<=\().*?(?=\))"
Dim command As String = "(AL01-RM)+(AL03-RM)+20.0"
Dim matches As MatchCollection = Regex.Matches(command, paren)

For Each m In matches

For i As Integer = 0 To dgv.Rows.Count - 1

If m.ToString = dgv.Rows(i).Cells(1).Value Then

Dim temp As String = Trim(dgv.Rows(i).Cells(2).Value)
command = command.Replace(m.ToString(), temp)


End If

Next

Next

RTB2.Text = (command)

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