0
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.

我只会把“他的”换成“她的”。如果我注释掉最后一点,那么我只会将“他”替换为“她”。任何帮助都将受到欢迎。

4

1 回答 1

2

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

要修复它,请将您的第一个替换保留为

RE6 = RE.Replace(strData, A)

但是将所有其他替代品更改为

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

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

于 2013-08-08T06:02:10.057 回答