1

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:

  1. team of experts
  2. team manager
  3. A big team

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.

2
  • Look up negative lookbehind. Commented Jan 2, 2017 at 18:44
  • Is it C#? Try Regex.Replace(s, @"(?<!hlhl\$\$@)\bteam\b", "hlhl$$$$@team") Commented Jan 2, 2017 at 18:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.