PDA

View Full Version : [SOLVED:] Find & Replace - 3 Digits Regex Sentences - into Heading 1 Style



dj44
08-22-2016, 03:38 PM
Hi Folks,

how's every one doing.:)

I have been wrestling with this regex problem.

You see the 3 digits - each one followed by a period -

Find all the 3 digit sentences - and make them into a heading 1 style is my task.


7.3.1 Alligator

7.3.2 Alligator mississippiensis

7.3.3 Crocodiles

7.3.4 Crocodylomorpha



I would like to do a find and replace, but my wheels have got stuck on the regex for the past few hours

can a pro help me please as, ive been round all the regex testers and well - i can't seem to make this work


If i can just find those digit sentences i can do a find and replace to make it into a heading style

so far my attempts


^\d+\.$

\d*\.?\d*

(?<=^| )\d+(\.\d+)?(?=$| )

^[0-9] . {1,3}


http://stackoverflow.com/questions/5570820/regex-allow-digits-and-a-single-dot

thank you for your help

dj

Paul_Hossler
08-22-2016, 06:20 PM
Word doesn't use 'real' RegEx




Option Explicit
Sub test()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Style = ActiveDocument.Styles("Heading 1")

.Text = "[0-9]{1,}.[0-9]{1,}.[0-9]{1,}*^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

dj44
08-22-2016, 06:37 PM
Paul,

what a champ!

Thanks ever so much, this is another area i am new at regex -

This is way too advanced for me and most of ones i made

from the testers failed flat when i put it in the find and replace in word - it wouldnt recognise the d and the S


Theres nothing regular about regex - pun intended :grinhalo:
[0-9]{1,}.[0-9]{1,}.[0-9]{1,}*^13


Grateful to you pro's who know this art -

Good monday evening all

thanks again Paul :beerchug:

gmaxey
08-23-2016, 12:00 PM
I'm not faulting Paul's code as it worked for you and solved your problem. However, you should be aware that it may cause an undesirable should #.#.# appear anywhere else in the document but not start a paragraph. Here is an alternative that uses the range object:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "[0-9]{1,}.[0-9]{1,}.[0-9]{1,}*^13"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
If oRng.Words(1).Start = oRng.Paragraphs(1).Range.Start Then
oRng.Paragraphs(1).Range.Style = "Heading 1"
oRng.Collapse wdCollapseEnd
End If
Wend
End With
lbl_Exit:
Exit Sub
End Sub

dj44
08-23-2016, 12:20 PM
Hi Greg,

how did you know?

what i did was to manually delete those one found elsewhere, the regex is too complicated for me as i tried many times to tame it, following the instructions, so i just let it be

So this is super bonus!!

Thanks ever so much

and good evening :biggrin:

dj

gmaxey
08-23-2016, 12:43 PM
I didn't know. Just bored and looking for something to post.

About the only way I can think of to prevent that using the find string is:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "^13[0-9]{1,}.[0-9]{1,}.[0-9]{1,}*^13"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
oRng.MoveStart wdCharacter, 1
oRng.Paragraphs(1).Range.Style = "Heading 1"
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub

But that would miss the first paragraph. There is usually more than one way to solve (or nearly solve a problem) and each can have advantages and disadvantages.

Paul_Hossler
08-23-2016, 12:58 PM
I'm not faulting Paul's code as it worked for you and solved your problem. However, you should be aware that it may cause an undesirable should #.#.# appear anywhere else in the document but not start a paragraph. Here is an alternative that uses the range object:


Yea, I like yours better since there's fewer built-in assumptions

gmaxey
08-23-2016, 01:12 PM
My last was full of holes. We can't just collapse the range but have to collapse then back it up by 1 character. We can handle the first paragraph by temporarily adding a empty paragraph prefix:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
oRng.InsertBefore vbCr
With oRng.Find
.ClearFormatting
.Text = "^13[0-9]{1,}.[0-9]{1,}.[0-9]{1,}*^13"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
oRng.MoveStart wdCharacter, 1
oRng.Paragraphs(1).Range.Style = "Heading 1"
oRng.Collapse wdCollapseEnd
oRng.End = oRng.End - 1
Wend
End With
ActiveDocument.Range.Characters(1).Delete
lbl_Exit:
Exit Sub
End Sub