.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification