roybalderama
roybalderama

Reputation: 1640

Regex Replace all found occurrences if the word not matches with the given prefix

How can I replace text value without matching given prefix of text??

For Example:

test hello world... I know hello world, this seems hello world..

then our replace value is "HI"

the text will be..

test hello world... I know HI, this seems HI..

Upvotes: 0

Views: 693

Answers (2)

COLD TOLD
COLD TOLD

Reputation: 13579

to be somewhat clever and to avoid regular expression you might do something like this

string v = s.Replace("hello world", "HI"); //replace hello world in all occurrences with HI 

string newstring  = v.Replace("test HI", "hello world"); //place hello world where hi is after test 

Upvotes: 0

Jim W
Jim W

Reputation: 5016

(?<!test\s)\bhello world\b

This assumes that you're interested in test when it proceeds directly.

Upvotes: 1

Related Questions