Jeremy wanted to change how his page numbers were punctuated. He needed to change instances such as ‘p152’, ‘p24’ etc. to ‘-p. 152.’, ‘-p. 24.’—that is, he wanted to add a dash in front of the ‘p’, a period and a space after the ‘p’, and a period at the end of the number. I didn’t know what his number range extended to, so I created the Find string to include any of one to four numbers.
This is an easy thing to do using Word’s wildcard find and replace, but there are some warnings:
- where you have p123-p125, you’ll get ‘-p. 123.–p. 125.’ (i.e. with two dashes—these can be easily found and replaced later using a standard find/replace)
- where you have p123-125, only the first instance will be replaced as it’s the only one with the ‘p’—you’ll get ‘-p. 123.-125’ (again, you can find <period><dash> later and replace it)
- I use Word for Windows—the commands may be a little different in Word for Mac.
- Press Ctrl+h to open the Find and Replace window.
- Click More to open the lower section of the window.
- Select the Use wildcards checkbox.
- In the Find what field, type: (p)([0-9]1,4) (you may find it easier to copy this then paste it into the Find field).
- In the Replace with field, type: -1. 2. (there’s a space after the first period).
- Click Find Next.
- Check that the item found is correct (i.e. matches the pattern of ‘p’ following immediately by one or more numbers), then click Replace.
- If you are very confident that this works for you, you can click Replace All, BUT with Replace All you don’t get the opportunity to manually correct variations, such as those listed in the warnings above.
How this works
- (p) looks for the letter ‘p’ (this is the first element)
- ([0-9]) looks for any number from 0 to 9 immediately following the ‘p’, and 1,4 looks for any length of number from one character to four (e.g. p5, p23, p123, p1234)—if the numbers are larger than this, increase the ‘4’ in this part, to a larger number (this is the second element)
- – adds a dash in front of 1, which represents the first element in the Find (i.e. the ‘p’)
- . 2. adds a period after the first element, then a space, then the second element (which is the one to four digit number), then another period.