tags:

views:

881

answers:

4

Hi,

I need a regex to replace hyphens in the middle of any word but not to touch leading or trailing ones or standalone ones. This is for use in .NET within Regex.Replace()

I've tried the following

\w[-]\w

but that also captures the character either side of the hyphen. As an example, what I need is for the following string

-test test-test -test

If the replace character was !, to become

-test test!test -test

Any help greatly received

Thanks

James

+1  A: 

In Perl (you didn't mention a language) this works:

/\w-\w/

for instance, try this one-liner:

mine:~> perl -e '$x="abc-def"; $x=~/\w(-)\w/; print "$x $1\n";'
abc-def -

or this one:

mine:~> perl -e '$x="abc-def"; print "$x\n";$x=~s/(\w)-(\w)/$1 $2/; print "$x\n";'
abc-def
abc def

Here's an example (from the comments) replacing all of the relevant '-' in the string:

mine:~> perl -e '$x="-this i-s a te-st-"; print "$x\n";$x=~s/(\w)-(\w)/$1$2/g; print "$x\n";'
-this i-s a te-st-
-this is a test-

Note that in this example I simply removed the dash while in the previous examples I replaced it with a space. I also added the g modifier to the substitution command to make it replace all instances of '-'.

Nathan Fellman
Try running it against '-this i-s a te-st-' - it should give '-this is a test-' but Javascript gives '-this - a tt-'
Paolo Bergantino
A: 

this regex matches all words with exactly 1 hyphen in it:

[a-zA-Z]+[-][a-zA-Z]+

this is equivalent to:

\w+[-]\w+

Your expression matches the letter before the hypen, the hyphen and the letter after the hyphen. It findes all the hyphens you want, even in words with more than one hyphen. How do you want to replace the hyphens? Can you be more specific about your goals?

Tobias Langner
+1  A: 
echo "- foo-bar - bar-foo -" | sed -e 's/\([[:alnum:]]\)-\([[:alnum:]]\)/\1\2/g'

In case you were using sed you might have used the wrong character class for matching alpha numerical expressions.

Hardy
+6  A: 

If I unserstand the question correctly, it's about matching the hyphen, not the words next to it. Here's a perl-like regex for that:

(?<=\w)-(?=\w)

That's a positive lookbehind assertion, one hyphen, and a positive lookahead assertion

Nikolai Ruhe
Great, thanks. That worked perfectly
James
You are very welcome :)
Nikolai Ruhe