VB.NET:需要正则表达式将返回字符替换为另一个字符串
我试图将从 Windows 窗体中的多行文本框中获取的字符串中的所有回车符替换为字符串 ",
,以便当我在一些 HTML 它可以正确显示。
"
Function Blah(ByVal strInput As String) As String
Dim rexCR As Object
rexCR = CreateObject("VBScript.RegExp")
rexCR.Pattern = "\r"
rexCR.Global = True
Blah = rexCR.Replace(strInput, ",<BR>")
End Function
尝试搜索以下任何字符,但仍然没有成功:\r|\n|\r\c|\cM|\x0d
问题似乎是函数/表达式没有检测到文本中的任何回车符,我不知道为什么? 我知道该函数可以工作,因为我可以在其中放置不同的表达式作为测试,并且没问题
有什么想法吗?
I'm trying to replace all the carriage return characters in a string obtained from a multi line text box in a Windows Form with the string ", <BR>"
so that when I use the string in some HTML it displays correctly.
Function Blah(ByVal strInput As String) As String
Dim rexCR As Object
rexCR = CreateObject("VBScript.RegExp")
rexCR.Pattern = "\r"
rexCR.Global = True
Blah = rexCR.Replace(strInput, ",<BR>")
End Function
Tried searching for any of the following characters and still no luck:\r|\n|\r\c|\cM|\x0d
Problem seems to be the function/expression is not detecting any carriage returns in the text and I have no idea why?
I know the function works as I can put a different expression in there as a test and it's OK
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有正则表达式的普通
strInput.Replace(vbCrLf,",
怎么样?")
How about normal
strInput.Replace(vbCrLf,",<BR>")
without regex?其他人已经为您的问题提供了很好的解决方案。作为一般性说明,我想解释一下,VB.NET 没有像 C# 那样的字符串转义序列(\n、\r、\t、...)。 VB.NET 中的字符串文字 类似于 C# 中的逐字字符串 -- 唯一可以转义的字符是双引号(加倍)。
相反,您必须使用 VB 常量(例如
vbCrLf
、vbTab
)或 .net 常量(例如Environment.NewLine
)和字符串连接(“Hello World”和 vbCrLf
而不是“Hello World\n”)。Others have already provided good solutions to your problem. As a general remark, I would like to explain that VB.NET does not have string escape sequences (\n, \r, \t, ...) like C#. String literals in VB.NET are similar to verbatim string literals in C# -- the only character that can be escaped is the double-quote (by doubling it).
Instead, you have to use the VB constants (e.g.
vbCrLf
,vbTab
) or .net constants (e.g.Environment.NewLine
) and string concatenation ("Hello World" & vbCrLf
instead of `"Hello World\n").怎么样:
How about: