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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With