【问题标题】:split special character and alphabets from a string从字符串中拆分特殊字符和字母
【发布时间】:2012-09-13 09:02:03
【问题描述】:

我有一个字符串值。我包含字母、特殊字符和数字以及空白的组合。但我只想检索数字。

my code
-------
Dim str1 As String = "!@!@#!$@#$#123456habAB^*^&(*)(_)()*("
    Dim str2 As String = Regex.Replace(str1, "[\[\]\\\^\$\.\|\?\*\+\(\)\{\}%,;><!@#&\-\+/d]", "")

    MsgBox(str2)


output am getting
-----------------
123456habAB_

expected output
---------------
123456

【问题讨论】:

    标签: .net regex vb.net


    【解决方案1】:

    试试这个代码,简短而简单:

    Dim str1 As String = "!@!@#!$@#$#123456habAB^*^&(*)(_)()*("
    Dim str2 As String = Regex.Replace(str1, "[^\d]", "")
    
    MsgBox(str2)
    

    您的解决方案的问题是您没有用空字符串替换“_”或字母。从字符串中替换所有非数字比显式替换所有字符更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2020-05-18
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多