I am sooo close. I am trying code a regex expression for Notepad++ to replace a dash with a space, ignoring dashes already with a pre/post space. I realize I could search/replace " - " with "foobarfoo" then search for "-" replacing for " " then converting "foobarfoo" back to " - ", but damnit - I'm trying to learn regex!
Here's my problem:
Adapter - BNC Male to BNC-Female, Right Angle
to
Adapter - BNC Male to BNC Female, Right Angle
(note the disappearing dash in "BNC Female")
The closest I am getting is using this:
/(?:[^( )])\-(?:[^( )])/g
but that results with it finding the single letter ahead, the dash, and the single letter following:
Adapter - BNC Male to BNC-Female, Right Angle
WHY is it selecting the pre/post characters? Is this not:
(?:[^( )])
find anything except a space (as a noncapturing group)...
\-
... that follows with a dash ...
(?:[^( )])
... and is followed by anything except a space(as a noncapturing group)
I get even closer is I replace the first term with (?=[^( )])
but if I change the third term to (?![^( )])
I'm back to where I started - just selecting the dash in between the two spaces. GRRRR.
More samples here at http://regexr.com/444i2
The most readable option for Notepad++ is an alternation based regex with a conditional replacement pattern:
Find What:
(\s-\s)|-
Replace With:
(?1$1: )
The
(\s-\s)|-
pattern either captures into Group 1 a whitespace,-
and a whitespace, or just matches-
. If Group 1 matches, its value is pasted back where it was (=this match is skipped), else the-
in other contexts is replaced with a space.See a demo below:
Another option is using nested lookarounds:
Find What:
(a space)
-(?<!\s-(?!=\s))
Replace With:
The pattern matches:
-
- a hyphen(?<!\s-(?!=\s))
- this negative lookbehind fails the match if its pattern matches the following pattern immediately to the left of the current location (that is, right after-
):\s
- a whitespace-
- a hyphen (this is necessary to make sure we get to the same place where the lookbehind was triggered)(?!=\s)
- the next char is a whitespace (this is a lookahead, it is not possible to make it a simple\s
as we cannot let the lookbehind move its pattern position matching, i.e. we can't let it try\s-\s
before and including-
(it would result in a true result).Use
\w(-)\w
to replace all hyphens surrounded by alphabetic characters, digits and underscores, or[^ ](-)[^ ]
to replace all hyphens surrounded by non-space characters.Both work fine in my Notepad++ version with all of your examples.
To ignore dashes already with a pre/post space you could use positive lookarounds to assert that what is on the left and on the right are a non whitespace character
\S
In the replacement use a space.
(?<=\S)-(?=\S)
Regex demo