Excel 2007中的VBA宏。我想在Excel VBA中使用正则表达式将“他”替换为“她”,将“他”替换为“她”,将“他”替换为“她”

时间:2013-08-08 05:38:26

标签: regex excel vba

Sub Test()
  Dim strTest As String
  Dim strTemp As String

  strTest = Sheet1.Cells(1, 1).Value
  MsgBox RE6(strTest)
  Sheet1.Cells(2, 1).Value = RE6(strTest)
End Sub

Function RE6(strData As String)    
     Dim RE As Object 'REMatches As Object
     Dim P As String, A As String
     Dim Q As String, B As String
     Dim R As String, C As String
     Dim S As String, D As String
     Dim T As String, E As String
     Dim U As String, F As String
     Dim V As String, G As String
     Dim W As String, H As String
     Dim N As Integer
     Set RE = CreateObject("vbscript.regexp")


    P = "(?:^|\b)He"
    A = "She"

    Q = "(?:^|\b)he"
    B = "she"

    R = "(?:^|\b)Him"
    C = "Her"

    S = "(?:^|\b)him"
    D = "her"

    T = "(?:^|\b)Himself"
    E = "Herself"

    U = "(?:^|\b)himself"
    F = "herself"

    V = "(?:^|\b)His"
    G = "Her"

    W = "(?:^|\b)his"
    H = "her"

'This section replaces "He" with"She"
    With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
        .Pattern = P
       End With
    RE6 = RE.Replace(strData, A)

'This section replaces "he" with "she"
      With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
     .Pattern = Q
    End With
    RE6 = RE.Replace(strData, B)
'
'This section replaces "Him" with "Her"
   With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
        .Pattern = R
    End With
    RE6 = RE.Replace(strData, C)

 'This section replaces "him" with "her"
   With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
        .Pattern = S
    End With
    RE6 = RE.Replace(strData, D)

'This section replaces "Himself" with "Herself"
   With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
        .Pattern = T
    End With
    RE6 = RE.Replace(strData, E)

'This section replaces "himself" with "herself"
   With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
        .Pattern = U
    End With
    RE6 = RE.Replace(strData, F)

'This section replaces "His" with "Her"
   With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
        .Pattern = V
    End With
    RE6 = RE.Replace(strData, G)

'This section replaces "his" with "her"
   With RE
        .MultiLine = True
        .Global = True
        .IgnoreCase = False
        .Pattern = W '
    RE6 = RE.Replace(strData, H)
    End With

End Function

当我在这段文字上运行此代码时:

James has settled effortlessly in his new class. He has shown seriousness and demonstrated traits of a serious student in the first half of the term. I am very optimistic that his positive attitude towards work, if he does not relent, will yield positive dividends. However, James needs to respond positively to prompts on getting himself better organised in school. I wish Him, him the best in the second half of the term.

我只用“她”代替“他的”。如果我评论最后一点,那么我只用“她”代替“他”。我们非常欢迎任何帮助。

1 个答案:

答案 0 :(得分:2)

问题是你在strData上反复进行替换,而不是每次替换的结果;也就是说,你取出原始字符串,将“He”替换为“She”,然后将其存储在RE6中。然后再次取出原始字符串,将“he”替换为“she”,然后将其存储在RE6中,覆盖第一个替换,依此类推等等。这就是为什么你只看到最后一次替换的结果。

要解决此问题,请将第一个替换为

RE6 = RE.Replace(strData, A)

但是将所有其他替换更改为

RE6 = RE.Replace(RE6, B) <-- do this for B-H

这将为您提供所需的输出。