2012-09-13 93 views
1

上面有一个字符串值。我包含了alphbets,特殊字符和数字以及whitspace的组合。但我想只检索数字。分割字符串中的特殊字符和字母

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

    MsgBox(str2) 


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

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

回答

2

试试这个代码,简短:

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

MsgBox(str2) 

与溶液中,你并没有取代“_”或字母与空字符串的问题。它更容易替换字符串中的所有非数字,而不是显式替换所有字符。

+0

非常感谢你的回复 – vps

+1

欢迎光临。如果有帮助,请将其标记为已回答。 – Harsh

+0

对不起,我忘了标记 – vps