BLip
BLip

Reputation: 19

Regular expression ignore strings start with a specific string

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.

Upvotes: 1

Views: 1671

Answers (1)

sazzad
sazzad

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

Related Questions