以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]如何正则取得字符串中的数字?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=57219)

--  作者:linshengqi
--  发布时间:2014/9/21 11:22:00
--  [求助]如何正则取得字符串中的数字?

有一字符串:654515956(2)、654514850(17)、654515951(1),想将括号内的数字取出来,成为"2、17、1"

如何弄?


--  作者:有点甜
--  发布时间:2014/9/21 11:26:00
--  
 Dim str As String = "654515956(2),654514850(17),654515951(1)"
Dim reg As new System.Text.RegularExpressions.Regex("(?<=[0-9]*\\({1})[0-9]+(?=\\){1})")
Dim mc As object = reg.Matches(str)
For i As Integer = 0 To mc.count - 1
    msgbox(mc(i).Value)
Next

--  作者:linshengqi
--  发布时间:2014/9/21 11:46:00
--  

谢谢老师。但我不想这么复杂,不是取出单个数字,是想变成一个字符串"2、17、1",该如何弄呢?


--  作者:有点甜
--  发布时间:2014/9/21 11:52:00
--  
 Dim str As String = "654515956(2),654514850(17),654515951(1)"
Dim reg As new System.Text.RegularExpressions.Regex("[0-9]*\\({1}")
str = reg.Replace(str, "").replace(")", "")
msgbox(str)

--  作者:lsy
--  发布时间:2014/9/21 12:41:00
--  
Dim str As String() = "654515956(2)、654514850(17)、654515951(1)".Split("、")
Dim s As String
For i As Integer = 0 To str.Length - 1
   s + = str(i).Remove(0, 10).Replace(")","、")
Next
MessageBox.Show(s.Trim("、"))

--  作者:tongliaozyr
--  发布时间:2014/9/21 15:28:00
--  
学习