Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys,

i am making another programming language and the difference in this one is that you can make a variable with its own name. my previous ones, to set a variable, you would have to type a piece of code similar to:

string1 = something
print string1

in my new language, i want to make that code like this:

#x: Hello World
print_var x

but the way that i want to do this, is not making a new variable, i want to make it finding the variable name. i want the source code to look something like this:

VB
for each a as string in textbox1.text.split(system.environment.newline())
     if a.contains("print_var ")
          variable = a.findstring(a.substring(10))
          textbox2.text = textbox2.text & vbcrlf & variable
     end if
next


could you please give me a piece of code that look similar to this ^ but can find the word?
Posted

The following code can be used to extract the value of variable from Text of TextBox1. Since, the variable name is taken from Index 10 using a.SubString(10)
it is better to use a.StartsWith method instead of a.contains method.
VB
For Each a As String In TextBox1.Text.Split(ControlChars.Lf)
    If a.StartsWith("print_var ") Then
        TextBox2.Text &= vbCrLf + System.Text.RegularExpressions.Regex.Match( _
            TextBox1.Text, String.Format("(?<=#{0}: ).*", a.Substring(10)), _
            RegexOptions.CultureInvariant).Value
    End If
Next

The (?<=#{0}: ) pattern matches #x: , but excludes it from the Match.Value.

[Edit] C# code added as sought in the comment to this solution [/Edit]

Please try the following code:

C#
foreach (string a in TextBox1.Text.Split('\n')) {
    if (a.StartsWith("print_var ")) {
        TextBox2.Text += "\r\n" + System.Text.RegularExpressions.Regex.Match(TextBox1.Text, string.Format("(?<=#{0}: ).*", a.Substring(10)), RegexOptions.CultureInvariant).Value;
    }
}
 
Share this answer
 
v4
Comments
Member 8378691 14-Apr-12 4:03am    
thanks man, this works! you don't have to do this, but in textbox2(when you run the code), there is a random enter at the start, but when you run the code multiple times, the enter doesn't keep coming. is this fixable? as i said, you have done enough, this is your choice if you want to do it.
VJ Reddy 14-Apr-12 4:10am    
If I understood correctly, there may be an enter in the beginning of TextBox2, which you want to avoid. If it is so, please see my Solution 2.
VJ Reddy 14-Apr-12 5:05am    
Did solution 2 serve the purpose?
Member 8378691 10-May-12 6:37am    
hey, i know this is a big ask, but are you able to change this to Visual C#? I have been working with visual c# for ages now and i am now changing back to c#. i tried using a converter but it doesn't change the vbcrlf, i can't find anything else that would work. Could you please just convert to c#?
VJ Reddy 10-May-12 13:15pm    
I think \r\n may work in C# for vbCrLf. I have added the C# code. Please see the solution.
If the enter (new line) is to be avoided in the beginning of TextBox2, then initialize TextBox2.Text to String.Empty and introduce vbCrLf conditionally as shown below.
VB
TextBox2.Text = String.Empty
For Each a As String In TextBox1.Text.Split(ControlChars.Lf)
    If a.StartsWith("print_var ") Then
        TextBox2.Text &= iif(String.IsNullOrEmpty(TextBox2.Text), _
            String.Empty, vbCrLf) & System.Text.RegularExpressions.Regex.Match( _
            TextBox1.Text, String.Format("(?<=#{0}: ).*", a.Substring(10)), _
            RegexOptions.CultureInvariant).Value
    End If
Next
 
Share this answer
 
v2
Comments
Member 8378691 21-Apr-12 6:46am    
thanks man, this is the best!
Maciej Los 10-May-12 13:45pm    
And another one +5!
VJ Reddy 10-May-12 20:14pm    
Thank you, losmac.

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