PDA

View Full Version : Word RegEx - Find String - Move 3 Words to the Left Between Quotes



saphire99
12-20-2015, 05:33 AM
Hello,

to all again. I hope every one is having a great relaxing weekend.

I have come back to seek some advice on a regex problem - I assume.

My document has these website code lines hidden somewhere. I need to copy and paste 3 words to the left between some quote marks, and remove the spaces.


Here is an example:

<a href="# "> 1.2 Science Exam - 2015 Progress </a>
<a href="# "> 5.5 Algebra Equations - 2015 Progress </a>


End Result

<a href="#1.2ScienceExam"> 1.2 Science Exam - 2015 Progress </a>
<a href="#5.5AlgebraEquations"> 5.5 Algebra Equations - 2015 Progress </a>


My document has a lot of these hidden somewhere - as you can imagine I have to search and find these little lines.

Then for each one copy and paste to the left - before you know it - I am getting tired fast, as I have lots more in my folder that need the same yikes. :fright:

I recorded a macro - all it spewed up was copy paste and move back wards - it was as good as a lemon in winter.

My Attempt




Dim StrOld As String, StrNew As String
Dim RngFind As Range, RngTxt As Range, i As Long

StrOldRegEx = "<ahref \b[^>]*>(.*?)</a> "


StrOldRegEx = " <a href="# "> * </a> " ' Another try


Set RngTxt = Selection.Range

For i = 0 To UBound(split(StrOld, ","))
Set RngFind = RngTxt.Duplicate
With RngFind.Find

While .Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=4
Selection.TypeBackspace
Selection.MoveLeft Unit:=wdCharacter, Count:=7
Selection.TypeBackspace
Selection.Copy
Selection.PasteAndFormat (wdFormatOriginalFormatting)





I wish the word macro recorder was more helpful - so i could work out how to search for things and then perform an action.

Any help really appreciated

thank you in advance to the great experts:bow:

Saphire

gmayor
12-20-2015, 07:15 AM
You cannot achieve this with the macro recorder. Assuming that the quoted strings are an accurate representation, then provided their are no similar, but unrelated strings the document the following should work:

Option Explicit

Sub FormatReferences()
Dim orng As Range, oBold As Range
Dim strText As String
Set orng = ActiveDocument.Range
With orng.Find
Do While .Execute(FindText:="<a href=" & Chr(34) & "# " & Chr(34) & "> ")
orng.MoveEndUntil "-"
strText = Mid(orng.Text, 14, Len(orng.Text))
strText = Replace(strText, Chr(32), "")
orng.Text = Replace(orng.Text, Chr(32) & Chr(34) & "> ", strText & Chr(34) & "> ")
Set oBold = orng
oBold.MoveStartUntil "#"
oBold.Start = oBold.Start + 1
oBold.MoveEndUntil Chr(34), wdBackward
oBold.Font.Bold = True
orng.Collapse 0
Loop
End With
lbl_Exit:
Exit Sub
End Sub

saphire99
12-20-2015, 07:48 AM
Dear Graham,

I am so excited, thank you!!:omg:

You are a talented genius programming guru.

It worked like sweet candy.

As you can imagine I found it really hard making friends with the regex - it was driving me mad.

thank you so much for your generosity - this is my favorite macro





Really Grateful

Have a wonderful holiday!

Saphire
xo

:wavey:

Please mark as solved