求职正则表达!提取括号中的内容~~~~~~~
如题:1( 10007 12) 23 122 389 388
就是要替换括号中的空格,而不影响外部的空格,最后变为
1(10007:12) 23 122 389 388
我的想法是先剔除(后的空格,变为
1(10007 12) 23 122 389 388
然后再替换括号中的空格变为
1(10007:12) 23 122 389 388
现在第一步已经实现,最后一步无法实现,求高手赐教!
ps:我用的是vb环境 --------------------编程问答-------------------- 把“( ”替换成“(”,用replace即可实现;
用instr从(开始找,找到空格替换为冒号,一直到字符为)止。 --------------------编程问答-------------------- --------------------编程问答-------------------- 如果楼主觉得括号内的第一个空格有删除的必要,则先处理掉它。
然后试试下面的这个代码:
Dim strTemp$--------------------编程问答-------------------- 提取括号内的内容的方法是:
strTemp = "1( 10007 12) 23 122 389 388"
' 如何要删除第一个空格,先加代码处理,再用下一句:
Mid$(strTemp, InStr(InStr(1, strTemp, "(") + 2, strTemp, " "), 1) = ":"
'此代码由“正则测试工具 v1.1.35”自动生成,请直接调用TestReg过程
Private Sub TestReg()--------------------编程问答-------------------- replace函数就是用来做这事的..... --------------------编程问答-------------------- 谢谢!我本意是想通过正则表达式来完成,四楼的回复比较靠谱,不过我测试了一下还有空格,但是起码提供了一个思路,可以对match再进行正则replace,就可以实现了!哈哈 --------------------编程问答-------------------- 结贴吧 --------------------编程问答--------------------
Dim strData As String
Dim reg As Object
Dim matchs As Object, match As Object
strData = "1( 10007 12) 23 122 389 388"
Set reg = CreateObject("vbscript.regExp")
reg.Global = True
reg.IgnoreCase = False
reg.MultiLine = True
reg.Pattern = "\((.*?)\)"
Set matchs = reg.Execute(strData)
For Each match In matchs
'Debug.Print match.Value
Debug.Print match.SubMatches(0)
Next
End Sub
--------------------编程问答--------------------
Dim s As String
s = "1( 10007 12) 23 122 389 388"
s = Split(s, "(")(0) & "(" & Replace(Replace(Trim(Split(Split(s, "(")(1), ")")(0)), " ", " "), " ", ":") & ")" & Split(s, ")")(1)
Debug.Print s
Option Explicit
Private Sub Form_Load()
Dim s As String, s1 As String
Dim m1 As Match
Dim ms1 As MatchCollection, sms1 As SubMatches
Dim objreg1 As RegExp, objreg2 As RegExp
Dim ret As String
s = "1(3 32 323 2343 34 ) 23 122( 200 3 43 23 65 74 452 4634 ) 389 388"
Set objreg1 = New RegExp
With objreg1
.IgnoreCase = True
.MultiLine = True
.Global = True
.Pattern = "([^\(|\)]*\()(.+?)(\)[^\)|\(]*)"
Set ms1 = .Execute(s)
For Each m1 In ms1
Set sms1 = m1.SubMatches
Set objreg2 = New RegExp
With objreg2
.IgnoreCase = True
.MultiLine = True
.Global = True
.Pattern = "(\s+)"
s1 = .Replace(Trim(sms1(1)), ":")
End With
ret = ret & Trim(sms1(0)) + s1 + Trim(sms1(2))
Next
End With
Debug.Print ret
End Sub
'输出
1(3:32:323:2343:34) 23 122(200:3:43:23:65:74:452:4634) 389 388
补充:VB , 基础类