Reputation: 19
I need to find all the words containing some string. For example "team" and replace it with another string. The string will as a substring for example:
I need to replace all those places with the string "hlhl$$@team"
.
I use the regular expression:
String exp= String.Format("({0}\\s)|({0}$)", "team);
The problem is that strings that are already are "hlhl$$@team"
match the regular expression and are being replaced to "hlhl$$@hlhl$$@team"
How can I ignore those strings that start with hlhl$$@
?
Thanks.
Upvotes: 1
Views: 1671
Reputation: 6267
Negative Lookbehind is your friend.
You want team
which is not preceded with hlhl$$@
. So the regex is
(?<!hlhl\$\$@)team
Here $
is required to escape because it is an special character in regex.
Upvotes: 2