如何找到段落的结尾(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
答案 0 :(得分:1)
这里绝对不需要使用正则表达式:只需用<br/>
替换所有换行符:
Protected Function MakeParagraph(ByVal txt As String) As String
Return txt.Replace(vbCrLf, "<br/>")
End Function
顺便说一句,如果你想创建HTML(看起来你想这样做),在替换换行符之前不要忘记HTMLEncode文本。
答案 1 :(得分: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