Regular expression replacement
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?
Try this crazy one:
-(?!\w(?<=\s-\w))
This regex:
- Searches for a dash that isn't followed by a (letter with a space two characters before it).
- Takes care of
test- test
and-test
, which you don't have in your test cases. - Selects only the dash, so you can replace it (this really what made the definition so complicated).
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, " ");
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.
精彩评论