2

如何找到段落的结尾(TextBox 中的用户键入 ENTER)?什么是正确的正则表达式?我正在使用这段代码:

    Protected Function MakeParagraph(ByVal txt As String) As String
    Dim regx As New Regex("/n", RegexOptions.IgnoreCase)        
    Dim matches As MatchCollection = regx.Matches(txt)

    For Each Match As Match In matches
        txt = txt.Replace(Match.Value, "<br/>")
    Next        
    Return txt
End Function

Protected Sub Button1_Click(sender As Object, e As EventArgs)
    Label1.Text = MakeParagraph(TextBox1.Text)
End Sub
4

2 回答 2

1

这里绝对不需要使用正则表达式:只需将所有换行符替换为<br/>

Protected Function MakeParagraph(ByVal txt As String) As String
    Return txt.Replace(vbCrLf, "<br/>")
End Function

而且,顺便说一句,如果你想创建 HTML(看起来你想这样做),不要忘记在替换换行符之前对文本进行HTMLEncode 。

于 2013-03-01T15:08:08.773 回答
0

为什么需要使用正则表达式?你不能做类似的事情:

Protected Sub Button1_Click(sender As Object, e As EventArgs)
    Label1.Text = TextBox1.Text.Replace(vbNewLine, "</br>")
End Sub

此外,您可以拆分每个段落并以这种方式处理它们:

Protected Sub Button1_Click(sender As Object, e As EventArgs)
    Dim Paragraphs() as String = TextBox1.Text.Split(vbNewLine)
    For Each Paragraph in Paragraphs
        'Do something with the paragraph ex:
        'Label1.Text &= Paragraph & "</br>"
    Next        
End Sub
于 2013-03-01T15:05:28.960 回答