Click here to Skip to main content
15,918,485 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I want to implement this feature :

If I have a following sentence :

{My name is James Vick and I am a {member|user|visitor} on this {forum|website|site} and I am loving it | I am admin and I am a {supervisor|admin|moderator} on this {forum|website|site} and I am loving it}

on a click of button I want to generate this :

My name is James Vick and I am a member on this site and I am loving it

or

I am admin and I am an admin on this forum and I am 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 do I 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?
Posted
Updated 3-Sep-10 5:27am
v2
Comments
Dalek Dave 3-Sep-10 11:27am    
Edited for Grammar and Spelling.

You have to change the regex and you have to iterate until all nested '{}' groups are processed.

The regex expression needs to be:
"{([^{}]+)}"<br />
. This matches any expression that begins with '{' but does not contain either '{' or '}' - ensuring that it exactly matches one of your options groups.

You need to feed the output result string back into the regex replace, iteratively, until it no longer changes. Each time you do the replace, spans of '{...}' gets replaced by one of its options, leaving their containing '{...}' spans 'visible' to the next replace attempt (they are now visible because there is no longer a '{' or '}' nestled within).

Here's my test app (note - I've hard-coded the selected option; you'll need to change it):

VB
Public Shared Sub RunTest()
    Dim re As New Regex("{([^{}]+)}")
    Dim result As String = "{My name is James Vick and I am a {member|user|visitor} on this {forum|website|site} and I am loving it | I am admin and I am a {supervisor|admin|moderator} on this {forum|website|site} and I am loving it}"
     
    Do While result.Contains("{")
        result = re.Replace(result, New MatchEvaluator(AddressOf RegexTest.ReplaceMatch))
    Loop
 
    Console.WriteLine(result)
    Console.Write("Press any key to exit: ")
    Console.ReadKey
End Sub
 
Private Shared Function ReplaceMatch(ByVal m As Match) As String
    Return m.Groups.Item(1).Value.Split(New Char() { "|"c })(0)
End Function

Note - instead of tracking 'did the string change' I'm just testing to see if it still contains a '{'. It's good enough for a test app, but may not be safe enough for your app.
 
Share this answer
 
Comments
amit_upadhyay 3-Sep-10 21:54pm    
wow!! works like a charm. Thanks.
Note : To randomize the changed strings i included this in replacematch :

Dim parts = m.Groups.Item(1).Value.Split(New Char() {"|"c})
Return parts(r.Next(0, parts.Length))

and this at top :

Private r As New Random()
amit_upadhyay 3-Sep-10 21:56pm    
Reason for my vote of 5
perfect answer
You're not planning to spam forums with this code are you? Generating random sentences? ;)
 
Share this answer
 
Comments
amit_upadhyay 3-Sep-10 21:40pm    
no, i saw this in a software and tried to implement it
amit_upadhyay 3-Sep-10 21:57pm    
Reason for my vote of 1
if you cant help then dont

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