What's wrong with this regular expression?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Crystal (VIP)
Hi,
I have a Regular Expression like this:

string s1=Regex.Replace("BA","A",@"C$&");

I assume that s1 will be "BC$&", however the real rusult is "BCA".
Is this a bug or my Replace method is wrong?
I am using .NET Framework 1.0.
Thanks!

Reply to this message...
 
    
Niki Estner
"Crystal" <Click here to reveal e-mail address> wrote in
news:Click here to reveal e-mail address...
[Original message clipped]

No, Replace is supposed to work like that.
See:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconsubstitutions.asp

Using "string s1=Regex.Replace("BA","A",@"C$$&");" should get you the
expected result.

Niki

Reply to this message...
 
    
Crystal (VIP)
Hi Niki,

I get the replacement string "C$&" from a text file and I can't modify it.
Is there another solution?
Thanks!

"Niki Estner" wrote:

[Original message clipped]

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Crystal <Click here to reveal e-mail address> wrote:
[Original message clipped]

Regex.Escape transforms "C$&" to "C\$&" - does that work?

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
James Curran
"Crystal" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

You may not be able to change the text file, but you can certainly read
the string you read out of it.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)

Reply to this message...
 
    
Niki Estner
"Crystal" <Click here to reveal e-mail address> wrote in
news:Click here to reveal e-mail address...
[Original message clipped]

Why can't you modify the string? Replacing '$' with '$$' in the substitution
string will do it.

> Is there another solution?

Not with Regex.Replace.

Niki

Reply to this message...
 
    
Crystal (VIP)
Hi there,

My text file is a financial report. There are thousands $ in it. "$&"
happens coincidently. If I change the replacement string, I have to change it
back.
Since there is no simple solution for this scenario, I have decided to use
string operation instead of Regular Expression.

Thanks,

"Niki Estner" wrote:

[Original message clipped]

Reply to this message...
 
    
Niki Estner
"Crystal" <Click here to reveal e-mail address> wrote in
news:Click here to reveal e-mail address...
[Original message clipped]

Maybe I was unclear about that: Regex.Replace will *always* replace "$$" to
"$". It's similar to "\\" in a string literal.

Niki

Reply to this message...
 
 
System.Text.RegularExpressions.Regex




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com