Regex and superscripts

A

Andrew Montford

I have written my first regex script to find

Numbers then maybe some spaces then o then C. (i.e. a temperature in degrees
C, but where a superscript o is used instead of a degree symbol). I realise
that regex can't search for superscript o as such, but with the case
sensitivity switched on, the match seems to work OK.

Then my script replaces the o with a degree symbol, and removes any spaces.

And it works!

The problem is that the code is also making all superscript and subscript
numbers in the rest of the document into plain text. I'm a complete newbie to
coding, so I'm not even sure if this is a bug or a feature.

Can anyone help? The code I've written is as follows:

Sub Degrees()

Dim re As RegExp
Dim para As Paragraph
Dim rng As Range
Set re = New RegExp
re.Pattern = "([0-9]+)([ ]?)([o])([C])"
re.IgnoreCase = False
re.Global = True
For Each para In ActiveDocument.Paragraphs
Set rng = para.Range
rng.MoveEnd Unit:=wdCharacter, Count:=-1
rng.Text = re.Replace(rng.Text, "$1°$4")
Next para
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top