View Single Post
 
Old 02-18-2025, 05:54 AM
batman1 batman1 is offline Windows 11 Office 2013
Advanced Beginner
 
Join Date: Jan 2025
Posts: 57
batman1 is on a distinguished road
Default

Quote:
Originally Posted by Ddadoo57 View Post
Today, to avoid losing the formatting with a regex, I use this type of code, but not everything is processed as I'd like.

Code:
               regex.Pattern = “([A-Za-zÀ-ÖØ-öø-ÿ0-9])\.([A-Za-zÀ-ÖØ-öø-ÿ0-9])”
                regex.Global = True
                If regex.test(texte.Text) Then
                    Set objMatches = regex.Execute(text.Text)
                    For Each objMatch In objMatches
                        With texte.Find
                            .ClearFormatting: .Replacement.ClearFormatting: .MatchWildcards = False
                            .Text = objMatch.Value
                            .Replacement.Text = Left(.Text, 1) & “. “ & Right(.Text, 1)
                            .Wrap = wdFindContinue: .Execute Replace:=wdReplaceAll
                        End With
                    Next
                    Set objMatches = Nothing
                 End If

Only regex without Find/Replace
Code:
Sub demo()
Const char = "…"
Dim i As Long, text As String, regex As Object, objMatch As Object, objMatches As Object
    text = ThisDocument.Range.text
    Set regex = CreateObject("VBScript.RegExp")
    regex.Global = True
    regex.Pattern = "(" & char & ")([A-Za-zA-ÖO-öo-y0-9])"
    If regex.test(text) Then
        Set objMatches = regex.Execute(text)
        For i = objMatches.Count - 1 To 0 Step -1
            Set objMatch = objMatches(i)
            ActiveDocument.Range(objMatch.FirstIndex, objMatch.FirstIndex + objMatch.Length).text = objMatch.SubMatches(0) & " " & objMatch.SubMatches(1)
        Next
    End If
    Set regex = Nothing
End Sub
Reply With Quote