Regex.Replace

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm looking for a pattern, for replacing whole words as long as they are not
preceded by "#(" ==> where # stands for any number

eg: (P is to be replaced with "ReplaceText")
Input: AVG(P/123(P)*PE/P+213(P))

Output: AVG(ReplaceText/123(P)*PE/ReplaceText+213(P))

Thanks,
UK.
 
UK said:
I'm looking for a pattern, for replacing whole words as long as they are
not
preceded by "#(" ==> where # stands for any number

Try: "(?<!\d\()P\b"

(?<!\d\() only matches if the string is not prefixed with a digit, followed
by a paren

\b matches for a word boundary.

I think this does what you want.

Niki
 
Niki,
I've c# code as below:
string Searchfor = "P";
string ReplaceWith = "TestReplace";
string Pattern = "(?<!\d\()" + searchFor + "\b";
Result = Regex.replace(Exp,pattern,replacewith);

where: Exp = AVG(P/123(P)*PE/P+213(P))
Result needed = AVG(TestReplace/123(P)*PE/TestReplace+213(P))

Now, Result will have the modified exp, Right?? Also, I'm getting compile
error - "Unrecognized escape sequence."

Thanks,
UK
 
UK said:
Niki,
I've c# code as below:
string Searchfor = "P";
string ReplaceWith = "TestReplace";
string Pattern = "(?<!\d\()" + searchFor + "\b";
Result = Regex.replace(Exp,pattern,replacewith);

where: Exp = AVG(P/123(P)*PE/P+213(P))
Result needed = AVG(TestReplace/123(P)*PE/TestReplace+213(P))

Now, Result will have the modified exp, Right?? Also, I'm getting compile
error - "Unrecognized escape sequence."

Either escape backslashes ("...\\...") or use verbatim strings (@"...\...").

Niki
 
Back
Top