469,955 Members | 1,878 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

regex replace problem.

I am trying to replace a string "P" with "\P" as long as the string "P" does
not already have a "\" in front.

for my search string I've used @"([^\\]P)" so my regex replace statement is:

System.Text.RegularExpressions.Regex pR = new
System.Text.RegularExpressions.Regex(@"([^\\]P)");
string strResult = pR.Replace(@"This is a XP test X\P", @"\P");

The poblem is that the statement replaces XP with \P where I want it to be
X\P. Can I do this with a regex statement?


Sep 4 '07 #1
2 1372
Hello Jeremy,
I am trying to replace a string "P" with "\P" as long as the string
"P" does not already have a "\" in front.

for my search string I've used @"([^\\]P)" so my regex replace
statement is:

System.Text.RegularExpressions.Regex pR = new
System.Text.RegularExpressions.Regex(@"([^\\]P)"); string strResult =
pR.Replace(@"This is a XP test X\P", @"\P");

The poblem is that the statement replaces XP with \P where I want it
to be X\P. Can I do this with a regex statement?
The easiest way is to use a negative look behind:

@"(?<!\)P"

The (?<! ... ) will check that the expression between the parentesis won't
match in front of the current position (e.g. in front of the P character).

Now you can easily replace with

@"\$0"

$0 will contain the contents of the matching regex, so if you later decide
you want to replace \R as well you can more easily change the expression to:

@"(?<!\)[PR]"

And the replace pattern will still work.

--
Jesse Houwing
jesse.houwing at sogeti.nl
Sep 4 '07 #2
Thank!

"Jesse Houwing" <je***********@newsgroup.nospamwrote in message
news:21**************************@news.microsoft.c om...
Hello Jeremy,
>I am trying to replace a string "P" with "\P" as long as the string
"P" does not already have a "\" in front.

for my search string I've used @"([^\\]P)" so my regex replace
statement is:

System.Text.RegularExpressions.Regex pR = new
System.Text.RegularExpressions.Regex(@"([^\\]P)"); string strResult =
pR.Replace(@"This is a XP test X\P", @"\P");

The poblem is that the statement replaces XP with \P where I want it
to be X\P. Can I do this with a regex statement?

The easiest way is to use a negative look behind:

@"(?<!\)P"

The (?<! ... ) will check that the expression between the parentesis won't
match in front of the current position (e.g. in front of the P character).

Now you can easily replace with
@"\$0"

$0 will contain the contents of the matching regex, so if you later decide
you want to replace \R as well you can more easily change the expression
to:

@"(?<!\)[PR]"

And the replace pattern will still work.

--
Jesse Houwing
jesse.houwing at sogeti.nl


Sep 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by bill tie | last post: by
2 posts views Thread by Jac | last post: by
6 posts views Thread by tshad | last post: by
17 posts views Thread by clintonG | last post: by
6 posts views Thread by Gary Bond | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by Karch | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.