Question problem with regex

jamesvick

Active member
Joined
Jul 19, 2010
Messages
34
Programming Experience
1-3
how do i find a pattern in richtextbox??

the text is like :

hello i want to {help|meet|call} you {please|come|soon}.

i want to find all such strings which have curly braces and then replace the whole string with any word from within it, RANDOMLY.

so on the click of a button the above line might become :

hello i want to help you please.
OR
hello i want to call you soon.

i thought of using regex but iam completely blank on how to use it, also i didnt understand some tutorials on google.

this method works
VB.NET:
Random r = new Random();
        private void butRegex_Click(object sender, EventArgs e)
            {
            string stringWithTextIn = "hello i want to {help|meet|call} you.";
            Regex regex = new Regex(@"({.*})");
            string r = regex.Replace(stringWithTextIn, new MatchEvaluator(ReplaceMatch));
            MessageBox.Show(r);
            }
        string ReplaceMatch(Match m)
            {
            string s = m.Value;                     // Get matched text only
            s = s.Substring(1, s.Length - 2);       // Remove '{' and '}'
            string[] parts = s.Split('|');          // Break into words
            return parts[r.Next(0,parts.Length)];   // return randow word
            }

but it works only when there is one variation i.e for sentences like

hello i want to {help|meet|call} you.

any idea what is wrong??
 
Hey, this is a VB.Net forum !! Use VB.Net language when you communicate here.
As for Regex learning I recommend this place Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
About your regex "({.*})" I think someone has played you a trick, try this one instead "{(.*?)}" and in MatchEvaluator do this:
VB.NET:
Dim parts = m.Groups(1).Value.Split("|"c)
Return parts(rnd.Next(0, parts.Length))
 
Might also have to escapre the girly brackets:

"\{(.*)\}"

because they are used in quantifying:

a{2,3}

== "at least 2 and at most 3 occurrences of 'a' "
 
alright now iam stuck with this :
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

i need two level regex expression. What will be the regex string??

i guess : {(.*?){(.*?)}}
 
I don't think that is possible with regex. You could use different kind of delimiters, or perhaps split string " | " first (ie it's different from "|" word delimiter string).
 
then how about if i have the outer string in [] and inner in {} like :

[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]

how to proceed then ?
 
Extract the [] blocks with regex and split by " | ", the sentence option you choose you handle like before for {} word options.
 
hi john i did the following :

Dim stringWithTextIn As String = RichEditControl1.Text
Dim regex2 As New Regex("[(.*?)]")
Dim r As String = regex2.Replace(stringWithTextIn, New MatchEvaluator(AddressOf ReplaceMatch))

and in replacematch

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 thought replacing {} with [] will atleast separate the text in [hi hello|bye] to give bye. But it is not working. Any idea of what iam doing wrong?

Also i forgot to ask why do we have a "c" in
Dim parts = m.Groups(1).Value.Split("|"c)
 
" "c means Char, as opposed to " " which is a String.

Let's clear things up a bit, wasn't the idea that you have a string that contains multiple [] blocks and inside them expressions separated by " | " strings? This is no different than the original problem where you had multiple {} block and inside them expressions separated by the "|" char. But DO see the difference between "|" and " | ".
 
alright : actually my original problem was multiple blocks in {} brackets. My new problem is multiple {} in SINGLE [] brackets.

Eg: [hello {hi|bye}|lloe]

you see i have multiple {} inside single [] brackets. What i thought is:

first separate block of [] bracket (same way we did for {}). Then whatever result i get separate that using our first code for {}.

but the regex is not working for a simple code like this (notice i changed regex for []) :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
richtextbox1.text="[hello|hi]"
Dim stringWithTextIn As String = RichTextBox1.Text
Dim regex As New System.Text.RegularExpressions.Regex("[(.*?)]")
Dim r As String = regex.Replace(stringWithTextIn, New MatchEvaluator(AddressOf ReplaceMatch))
RichTextBox1.AppendText(vbCrLf & r)
End Sub

Private Function ReplaceMatch(ByVal m As Match) As String
Dim parts = m.Groups(1).Value.Split("|")
Return parts(r.Next(0, parts.Length))
End Function

i also tried for " | ". Not working.
 
My new problem is multiple {} in SINGLE [] brackets.
I don't understand. What would be the purpose of [] then?
 
actually i am making an article spinner. Its for my article marketing needs. I want to spin articles with this application. What i do is load an article, select each word and replace with their synonyms. The uniqueness depends on how many words we spin. The next step would be to spin sentences and then the words within them. see a video for an example software :
YouTube - Magic Article Rewriter

he has been able to this thing with same {} brackets. I wouldn't mind [] for outer sentences. But iam not getting it.
 
It was so messy I didn't understand the structure, sorry :)
 
Back
Top