在richtextbox中双旋转文本 [英] double spin text in richtextbox

查看:128
本文介绍了在richtextbox中双旋转文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现此功能:

如果我有以下句子:

{我叫James Vick,我是这个{forum | website | site}上的{member | user | visitor},我很喜欢|我是管理员,并且是此{forum | website | site}上的{supervisor | admin |主持人},我喜欢它}

在单击按钮时,我想生成它:

我叫James Vick,我是该网站的成员,我很喜欢它.



我是管理员,也是这个论坛的管理员,我喜欢它

基本需求是在{|}大括号之间随机选择单词

我已经能够针对内部嵌套(例如,{member | user | visitor})执行此操作.但是,我该如何同时包含句子的外层嵌套,即{something blah blah {blah1 | blah2}和|其他东西}?

内部嵌套代码:

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?

推荐答案

您必须更改正则表达式,并且必须迭代直到所有嵌套的"{}"组均已处理.

正则表达式的表达式必须为:
You have to change the regex and you have to iterate until all nested ''{}'' groups are processed.

The regex expression needs to be:
"{([^{}]+)}"<br />

.该表达式匹配以"{"开头但不包含"{"或}"的任何表达式-确保它与您的选项组之一完全匹配.

您需要迭代地将输出结果字符串反馈到regex replace中,直到不再更改为止.每次执行替换时,"{...}"的跨度都会被其选项之一替换,而将包含"{...}"跨度的可见"保留给下一次替换尝试(它们现在可见了,因为其中不再包含"{"或}".)

这是我的测试应用程序(请注意-我已对所选选项进行了硬编码;您将需要对其进行更改):

. 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):

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.


您不打算使用此代码向论坛发送垃圾邮件吗?生成随机句子? ;)
You''re not planning to spam forums with this code are you? Generating random sentences? ;)


这篇关于在richtextbox中双旋转文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆