Remove hyphens except between numbers

DB Novice
Lounger
Posts: 49
Joined: 14 Jan 2015, 23:04

Remove hyphens except between numbers

Post by DB Novice »

How do I remove all hyphens except when the hyphen occurs between numbers?

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

Re: Remove hyphens except between numbers

Post by HansV »

Press Ctrl+H to activate the Replace dialog.
Click 'More >>' to display the options.
Tick the check box 'Use widcards'.
In the 'Find what' box, enter:

([!0-9])-([!0-9])

In the 'Replace with' box, enter

\1\2

Click 'Replace All'.

Explanation: when using wildcards, [!0-9] means: any character except the digits 0 to 9.
Each part of the 'Find what' text enclosed in parentheses ( ) defines a part of the text being found. In the 'Replace with' box, you can refer to the first such part as \1, the second one as \2 etc.

So 'Find what' specifies: any character but a digit (part #1), followed by a hyphen, then any character but a digit (part #2).
'Replace with' simply joins part #1 and part #2 without the hyphen in between.
Best wishes,
Hans

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

Re: Remove hyphens except between numbers

Post by StuartR »

You can do this in two replace commands. One will replace anything that has a nondigit followed by a hyphen with just the nondigit. The second will replace a hyphen followed by a nondigit with just the nondigit.

Firstly
Use Wildcards.
Find what: ([!1-9])-
Replace with \1

Then
Use Wildcards.
Find what: -([!1-9])
Replace with \1
StuartR


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

Re: Remove hyphens except between numbers

Post by StuartR »

The difference between Hans suggestion and mine is whether you want to keep the hyphen when it is between a digit and a letter
StuartR


DB Novice
Lounger
Posts: 49
Joined: 14 Jan 2015, 23:04

Re: Remove hyphens except between numbers

Post by DB Novice »

Thank you, gentlemen! That worked. You have saved me from extreme tedium!

May the Lord richly bless you for your service!