Find/Replace Codes

jmt356
SilverLounger
Posts: 2391
Joined: 28 Mar 2010, 01:49

Find/Replace Codes

Post by jmt356 »

The code to find any 1 digit number in a selection is [0-9]{1}, with wildcards enabled. To find any 2 digit number in a selection, the code is [0-9]{2}. For three digits, [0-9]{3}.

What is the code to find any 1 or 2 or 3 digit number in a selection?

Also, is there a code to find any 1 or 2 or 3 digit roman numeral (I, II, III, IV, etc.) in a selection?

Regards,

JMT
Regards,

JMT

User avatar
StuartR
Administrator
Posts: 12618
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Find/Replace Codes

Post by StuartR »

  • To find a single 1 digit number you just need to use [0-9]
  • To find any 1, 2 or 3 digit number you can search for [0-9]{1,3}
  • You can get close to what you are after for Roman numbers using [IVXLDCM]{1,3} but this will match quite a lot of strings that are not valid roman numbers, such as VIM
StuartR


jmt356
SilverLounger
Posts: 2391
Joined: 28 Mar 2010, 01:49

Re: Find/Replace Codes

Post by jmt356 »

Thank you.

Doesn't "*" stand for any number of unknown characters? Why then is it that when I search for "Chapter *" with wildcards selected, rather than get "Chapter I," "Chapter II," "Chapter III," etc., I only get "Chapter "?

When I tried searching with a ~ preceeding the *, I got no results in my search.

JMT
Regards,

JMT

User avatar
StuartR
Administrator
Posts: 12618
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Find/Replace Codes

Post by StuartR »

I assume you have just included the "" characters in this post to distinguish your search string, and that they are not typed into the Find what... dialog box.

Searching for "Chapter *" is a bit strange, I would expect it to find any instance of "Chapter " anywhere in the document since there is nothing after the * in your search string.

What are you actually trying to achieve?
Does the word chapter appear as normal text, or is it part of some heading style?
StuartR


jmt356
SilverLounger
Posts: 2391
Joined: 28 Mar 2010, 01:49

Re: Find/Replace Codes

Post by jmt356 »

I am trying to follow any appearance of "Chapter" followed by a roman numeral and a period and then replace with with "Chapter" followed by the same roman numeral and a hard enter.

I want to use the asterisk because it will pick up any roman numeral, no matter how character it is, between the word "Chapter" and the period (there is also a space between the word "Chapter" and the Roman numeral. Yet asterisk doesn't appear to be doing anything for me.
Regards,

JMT

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find/Replace Codes

Post by HansV »

Searching for "Chapter *" (without the quotes) is too vague, since * stands for ANY number of characters. So in theory, it would be from the first occurrence of the word "Chapter" to the end of the document. Word protects you against this. If there will be a space after the number, you could search for "Chapter * " (again, without the quotes). Otherwise, try Stuart's suggestion of looking for "Chapter [IVXLDCM]{1,3}" (without the, you get it).
Best wishes,
Hans

jmt356
SilverLounger
Posts: 2391
Joined: 28 Mar 2010, 01:49

Re: Find/Replace Codes

Post by jmt356 »

Shouldn't "Chapter *." (without the quotes) work?
Regards,

JMT

User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find/Replace Codes

Post by HansV »

If there will always be a period after the (roman) number, you can indeed use "Chapter *." (without the quotes) in the Find what box.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12618
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Find/Replace Codes

Post by StuartR »

jmt356 wrote:Shouldn't "Chapter *." (without the quotes) work?
But make sure you don't get any incorrect matches from sentences like...

"In this publication, the word Chapter often occurs in places where you would normally expect to see the word Section."

or anything else that is not your real target.
StuartR


jmt356
SilverLounger
Posts: 2391
Joined: 28 Mar 2010, 01:49

Re: Find/Replace Codes

Post by jmt356 »

It worked with the period and in the replace field I used "\1^13" (without quotes) so that I can get a paragraph break after each appearance of the word "Chapter."

However, these paragraph breaks do not behave correctly. For example, when I change the style of the word "chapter," the style of the text below (after the paragraph break) also changes. Also, if i put my cursor before the word "Chapter" and then, holding down shift + control, press the right arrow, rather the word "Chapter" and the entire paragraph below get selected.

Why is this?
Regards,

JMT

User avatar
StuartR
Administrator
Posts: 12618
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Find/Replace Codes

Post by StuartR »

If I search for
(Chapter )*.
and replace it with
\1^13

Then it adds a new paragraph immediately after the .
I needed to include () around the word Chapter and the following space to get this to work, I assume you did the same.
One possible reason for the formatting of the second paragraph changing is if you have "automatically update" set for the style of this paragraph.
StuartR


User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find/Replace Codes

Post by HansV »

I do see the strange behavior of the paragraph marks - it's as if the two paragraphs still belong together. (And no, it's not due to "Automatically update".) I don't know what causes this.

Here is a macro that appears to work better - at least when I try it:

Code: Select all

Sub ReplacePeriodsWithParagraphMarks()
  Selection.HomeKey Unit:=wdStory
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Chapter *."
    .Replacement.Text = ""
    .MatchWildcards = True
    .Wrap = wdFindStop
    Do While .Execute
      Selection.Collapse Direction:=wdCollapseEnd
      Selection.Delete Count:=-1
      Selection.TypeParagraph
    Loop
  End With
End Sub
Best wishes,
Hans

jmt356
SilverLounger
Posts: 2391
Joined: 28 Mar 2010, 01:49

Re: Find/Replace Codes

Post by jmt356 »

The ^13 function fails to do something that ^p does do. When I am using ^13, Word is treating it in some ways like ^l, but when I show the hidden characters, there is indeed a paragraph mark. yet even teh spacing between the paragraphs doesn't show up because Word things that the paragraph preceding the ^13 and that following it are united by a soft paragraph break.
When I search for all instances of ^13 and replace with ^p, the behave as normal paragraph breaks.
Regards,

JMT

User avatar
StuartR
Administrator
Posts: 12618
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Find/Replace Codes

Post by StuartR »

HansV wrote:I do see the strange behavior of the paragraph marks - it's as if the two paragraphs still belong together. (And no, it's not due to "Automatically update".) I don't know what causes this...
Strange. It definitely worked correctly for me.
I wonder if this is something they have fixed in Word 2010.
StuartR


User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find/Replace Codes

Post by HansV »

Here is a screenshot from Word 2003.

The paragraph mark after Chapter 1 was inserted by the wildcard find/replace.
The paragraph mark after Chapter 2 was inserted manually.
You can see that the first one doesn't preserve the "Space After" of the Heading 1 style. Although it looks like a paragraph mark (and its ANSI code is indeed 13) it acts like a hard line break.
x95.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12618
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Find/Replace Codes

Post by StuartR »

HansV wrote:Here is a screenshot from Word 2003...
Yes, I do see that behaviour with Word 2010, but applying formatting to the first paragraph doesn't affect the second one.
StuartR


User avatar
HansV
Administrator
Posts: 78558
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find/Replace Codes

Post by HansV »

Just a half-baked bug fix, apparently...
Best wishes,
Hans