2012-12-22 88 views
4

是否可以遍歷VBA RegEx匹配項並用ID值給定的特定數據替換?迭代通過匹配並替換爲VBA正則表達式

例如,

<a id="a-UP:124" {REPLACEITEMHERE} 
.../a> 

與我的模式是這樣的:

<a id="a-([\w\d]+:[\w\d]+)" ({REPLACEITEMHERE}) 

所以我有多個 「替代項目」 各是唯一的的UP:124值。

這可能在VBA RegEx?只是想在我經歷一個更麻煩的過程之前知道!謝謝!

更新(根據評論者的要求提供更多細節 - 希望這可以讓我更清楚我在找什麼!我瞭解如何創建模式,更通過結果迭代,然後對每個發現我有麻煩謝謝)!

這是我使用的正則表達式:

<a id="a-([\w\d]+:[\w\d]+)"[^{]+({FILE})[^{]+({PERCENT})[^{]+({COLOR}) 

這樣的設置爲:

.Global = True 
.IgnoreCase = True 
.MultiLine = False 

的替換模式,我想是檢查什麼第一捕獲組$1的值,然後與我已存儲在一個類中的適當的值替換值{FILE}{PERCENT}{COLOR}(組$2$3,和$4) 。

<path 
    style="fill:#d40000;fill-opacity:1;filter:url(#filter5248)" 
    d="m 168.04373,162.08375 c -4.7586,-5.00473 -8.65201,-9.35811 -8.65201,-9.67419 0,-0.81973 18.30811,-16.3921 25.16949,-21.40847 7.11903,-5.20474 16.462,-10.93031 17.83606,-10.93031 0.56369,0 3.81291,5.56174 7.22048,12.35942 l 6.19558,12.35941 -7.13301,3.9009 c -7.96536,4.3561 -21.53264,13.83148 -27.5305,19.22729 -2.16466,1.94738 -4.05237,3.47876 -4.19491,3.40307 -0.14254,-0.0757 -4.15257,-4.2324 -8.91118,-9.23712 z" 
    id="path5246" 
    inkscape:connector-curvature="0" 
    transform="matrix(0.8,0,0,-0.8,0,792)" /> 
<a id="a-UP:115E" 
xlink:href="{FILE}" 
xlink:title="UP:115E 
{PERCENT}%"> 
<path 
id="UP:115E" 
style="fill:{COLOR};fill-opacity:1;stroke:none" 

    d="m 272.81031,529.10942 c 0.32799,18.973 -0.6558,38.48935 0.49159,57.12295 13.02609,-0.33792 26.60749,0.66479 39.29456,-0.4916 -0.32799,-18.973 0.6558,-38.48935 -0.49159,-57.12294 -13.01823,0.33523 -26.61862,-0.66099 -39.29456,0.49159 z" 


    inkscape:connector-curvature="0" 
    transform="matrix(0.8,0,0,-0.8,0,792)" /> 
</a> 
<a id="a-UP:115D" 
xlink:href="{FILE}" 
xlink:title="UP:115D 
{PERCENT}%"> 
<path 
id="UP:115D" 
style="fill:{COLOR};fill-opacity:1;stroke:none" 

    d="m 314.75946,529.10942 c 0.32799,18.973 -0.6558,38.48935 0.4916,57.12295 9.11694,0.926 18.85965,-1.04961 27.69299,0.721 -0.31086,4.08011 6.71077,4.04524 8.35706,1.67141 -0.0756,-1.75206 -3.96676,-2.62149 0,-2.32687 8.75271,2.70871 7.9153,-4.7371 7.43942,-11.04442 -0.32811,-15.47719 0.65596,-31.4979 -0.49159,-46.63566 -14.41803,0.33385 -29.41334,-0.65954 -43.48948,0.49159 z" 


    inkscape:connector-curvature="0" 
    transform="matrix(0.8,0,0,-0.8,0,792)" /> 
</a> 
</g></svg> 
+1

我想是的,但你可以給替換之前和替換之後的字符串的完整示例?謝謝 – Larry

+0

看看這個使用正則表達式替換字符串的例子:http://www.tmehta.com/regexp/index.htm –

+0

像拉里一樣,我認爲是的,但是你的問題太過於模糊了。 – brettdj

回答

3

我想你可以做這樣的事情簡單的例子(給你輸入的字符串是很難建立測試)

  • 打開GlobalFalse使用單一Regexp每個更換(另一種方法是使用Global = True但隨後針對不同的第一場比賽運行各種Regexps
  • 使用Do循環測試有效Regexp是否仍然
  • 測試第一submatch,然後用Select Case運行不同的程序,以取代submatches $2-$4(我存儲在一個簡單的數組)

代碼

Sub TestSub() 
    Dim strIn As String 
    Dim objRegex As Object 
    Dim objRegMC As Object 
    Dim objRegM As Object 
    Dim vArray(1 To 3, 1 To 2) 
    vArray(1, 1) = "No 1a" 
    vArray(2, 1) = "No 2a" 
    vArray(3, 1) = "No 3a" 
    vArray(1, 2) = "number 1b" 
    vArray(2, 2) = "number 2b" 
    vArray(3, 2) = "number 3b" 

    Set objRegex = CreateObject("vbscript.regexp") 
    strIn = "a12stuff notme b34other missthis" 
    With objRegex 
     .Pattern = "([a-z]{1})(\d)(\d)([a-z]+)" 
     .Global = False 
     .IgnoreCase = True 
     .MultiLine = False 
     Do While .test(strIn) 
      Set objRegMC = .Execute(strIn) 
      For Each objRegM In objRegMC 
       Select Case objRegM.submatches(0) 
       Case "a" 
        strIn = .Replace(strIn, "$1" & vArray(1, 1) & vArray(2, 1) & vArray(3, 1)) 
       Case "b" 
        strIn = .Replace(strIn, "$1" & vArray(1, 2) & vArray(2, 2) & vArray(3, 2)) 
       End Select 
      Next 
     Loop 
    End With 
    MsgBox strIn 
End Sub