Remove hyphens except between numbers
-
- Lounger
- Posts: 49
- Joined: 14 Jan 2015, 23:04
Remove hyphens except between numbers
How do I remove all hyphens except when the hyphen occurs between numbers?
-
- Administrator
- Posts: 78416
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Remove hyphens except between numbers
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.
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
Hans
-
- Administrator
- Posts: 12601
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Remove hyphens except between numbers
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
Firstly
Use Wildcards.
Find what: ([!1-9])-
Replace with \1
Then
Use Wildcards.
Find what: -([!1-9])
Replace with \1
StuartR
-
- Administrator
- Posts: 12601
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Remove hyphens except between numbers
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
-
- Lounger
- Posts: 49
- Joined: 14 Jan 2015, 23:04
Re: Remove hyphens except between numbers
Thank you, gentlemen! That worked. You have saved me from extreme tedium!
May the Lord richly bless you for your service!
May the Lord richly bless you for your service!