VB.NET:需要正则表达式将返回字符替换为另一个字符串

发布于 2024-08-18 19:35:43 字数 503 浏览 3 评论 0原文

我试图将从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

櫻之舞 2024-08-25 19:35:43

没有正则表达式的普通 strInput.Replace(vbCrLf,",
")
怎么样?

How about normal strInput.Replace(vbCrLf,",<BR>") without regex?

撩动你心 2024-08-25 19:35:43

其他人已经为您的问题提供了很好的解决方案。作为一般性说明,我想解释一下,VB.NET 没有像 C# 那样的字符串转义序列(\n、\r、\t、...)。 VB.NET 中的字符串文字 类似于 C# 中的逐字字符串 -- 唯一可以转义的字符是双引号(加倍)。

相反,您必须使用 VB 常量(例如 vbCrLfvbTab)或 .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").

各自安好 2024-08-25 19:35:43

怎么样:

Function Blah(ByVal strInput As String) As String
  return strInput.Replace(Environment.NewLine, "<br />")
End Function

How about:

Function Blah(ByVal strInput As String) As String
  return strInput.Replace(Environment.NewLine, "<br />")
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文