i want to implement this feature :

if i have a following sentence :

{my name is james vick and iam a {member|user|visitor} on this {forum|website|site} and iam loving it | iam admin and iam a {supervisor|admin|moderator} on this {forum|website|site} and iam loving it}

on a click of button i want to generate this :

my name is james vick and iam a member on this site and iam loving it

or

iam admin and iam a admin on this forum and iam loving it

The basic need is to randomly choose words between {|} braces

I have been able to do this for inner nest i.e. for {member|user|visitor}. But how to include the outer nest of sentences also i.e. {something blah blah {blah1|blah2} and | something else} ?

code for inner nest :

Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton3.Click
        Dim stringWithTextIn As String = RichTextBox1.Text
        Dim regex As New Regex("{(.*?)}")
        Dim r As String = regex.Replace(stringWithTextIn, New MatchEvaluator(AddressOf ReplaceMatch))
        RichTextBox2.Text = r
    End Sub
Private Function ReplaceMatch(ByVal m As Match) As String
        Dim parts = m.Groups(1).Value.Split("|"c)
        Return parts(r.Next(0, parts.Length))
    End Function

i know i just have to change the regex but what do i add in it to include outer braces?

There are many ways to do it, and one of them is using the recursivity; using it, you can define a parser function like

Function Parse(ByVal SourceString As String) As String
		'
		'	Create a temp return info
		'
		Dim ReturnString As New Text.StringBuilder
		'
		'	The loop position in the source string
		'
		Dim I As Integer = 0
		'
		'	Sart a new random sequence
		'
		Randomize()
		'
		'	While there is something in the source
		'
		Do While I < SourceString.Length
			'
			'	Get the char to process
			'
			Dim c As Char = SourceString.Chars(I)
			'
			'	Depending on the char
			'
			Select Case c
				Case "{"c
					'
					'	This is an opening so must start a deeper level
					'
					ReturnString.Append(Parse(SourceString.Substring(I + 1)))
					Exit Do
				Case "}"c
					'
					'	This is a closing one.
					'--------------------------------------------------------
					'
					'	calculate the options using the | separator
					'
					Dim Options As String() = ReturnString.ToString.Split("|"c)
					'
					'	Clear the return string to get an option. 
					'
					ReturnString = New Text.StringBuilder
					'
					'	Get the option
					'
					ReturnString.Append(Options(CType(Math.Floor(Rnd() * CType(Options.Length, Single)), Integer)))
					'
					'	Get hte rest of the string past the closing bracket
					'
					If I < SourceString.Length - 1 Then
						'
						'	Disregard all the source before the ending bracket and reset the pointer
						'
						SourceString = SourceString.Substring(I + 1)
						I = -1
					Else
						'
						'	If this is the last char, then nothing more to analyze
						'
						SourceString = ""
					End If
				Case Else
					'
					'	this is a normal char
					'
					ReturnString.Append(c)
			End Select
			'
			'	Go for the next char
			'
			I += 1
		Loop
		'
		'	Return the valid string
		'
		Return ReturnString.ToString

	End Function

then you can call the function using

Dim S As String = Parse("{my name is james vick and iam a {member|user|visitor} on this {forum|website|site} and iam loving it | iam admin and iam a {supervisor|admin|moderator} on this {forum|website|site} and iam loving it}")
		'
		'	Then randomly split the parsed text
		'
		'
		'	calculate the options using the | separator
		'
		Dim Options As String() = S.Split("|"c)
		'
		'	And get the final option
		'
		S = Options(CType(Math.Floor(Rnd() * CType(Options.Length, Single)), Integer))

Hope this helps

This article has been dead for over six months. Start a new discussion instead.