Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » .NET Programming (VB 2010 or earlier) » Visual Basic .NET » regex doubt


regex doubtExpand / Collapse
Author
Message
Posted 7/31/2010 12:21:34 PM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: 4/22/2011 10:39:32 PM
Posts: 11, Visits: 13
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
[code]
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
}[/code]

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??

working on a new one.
Post #29482
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Admin, Mod, Mark, Keithuk

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 3:38am