Reputation: 7359
I ve got the following reg exp
(-[^\w+])|([\w+]-[\w+])
I want to use it to replace dashes with a whitespace
test -test should not be replaced
test - test should be replaced
test-test should be replaced
So only if test -test the dash should NOT be replaced.
Currently ([\w+]-[\w+]) is replacing the t's around the dash.
var specialCharsExcept = new Regex(@"([\w+]-[\w+])", RegexOptions.IgnoreCase);
if (string.IsNullOrEmpty(term))
return "";
return specialCharsExcept.Replace(term, " ");
Any help? Thanks in advance
PS: I am using C#.
Update
I'm trying to use your reg exp for the following case now.
some - test "some test" - everything within the quotes the expression should not be applied
Is this possible?
Upvotes: 2
Views: 267
Reputation: 6451
Ok, changed according to comment.
>>> r = ' +(-) +|(?<=\w)-(?=\w)'
>>> re.sub(r, ' ', 'test - test')
'test test'
>>> re.sub(r, ' ', 'test-test')
'test test'
>>> re.sub(r, ' ', 'test -test')
'test -test'
EDIT Corrected according to comment. The trick is to add the 'lookahead assertion' with ?=
and the lookbehind with ?<=
, which not be part of the match, but will be checked.
Upvotes: 1
Reputation: 137997
Try this crazy one:
-(?!\w(?<=\s-\w))
This regex:
test- test
and -test
, which you don't have in your test cases.By the way - you don't need RegexOptions.IgnoreCase
because your regex has no literal parts, you aren't tryting to captrue /test/
from "Test TEST".
This will do:
Regex specialCharsExcept = new Regex(@"-(?!\w(?<=\s-\w))");
return specialCharsExcept.Replace(term, " ");
Upvotes: 5