By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Re: regex question

P: n/a
On Wed, 09 Jul 2008 23:02:32 -0700, CSharper <cs******@gmx.comwrote:
I have the following code and the first expression get replaced
properly but the second one does not replace at all. Only difference
in the two, the later one has escape sequences inside the string.

[...]
What could be the problem?
You don't need to escape the backslash for the C# compiler, but you still
need to do it for the Regex class.

Pete
Jul 10 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jul 10, 1:04*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 09 Jul 2008 23:02:32 -0700, CSharper <cshar...@gmx.comwrote:
I have the following code and the first expression get replaced
properly but the second one does not replace at all. Only difference
in the two, the later one has escape sequences inside the string.
[...]
What could be the problem?

You don't need to escape the backslash for the C# compiler, but you still*
need to do it for the Regex class.

Pete
I am sorry I lost you. The source is coming from a data file (for
simplicity, I just put the hard coded value. Same goes for the key, it
is also coming from a data file). I lost you in the compiler portion.
Jul 10 '08 #2

P: n/a
On Wed, 09 Jul 2008 23:09:47 -0700, CSharper <cs******@gmx.comwrote:
I am sorry I lost you. The source is coming from a data file (for
simplicity, I just put the hard coded value. Same goes for the key, it
is also coming from a data file). I lost you in the compiler portion.
The code you posted used the "@" symbol so that you don't have to escape
the backslash. That's all I meant.

No matter where you get the text from, you need to escape special Regex
characters, such as the backslash. Try using the Regex.Escape() method on
your key string before passing it to the Regex class.

Pete
Jul 10 '08 #3

P: n/a
On Jul 10, 1:37*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 09 Jul 2008 23:09:47 -0700, CSharper <cshar...@gmx.comwrote:
I am sorry I lost you. The source is coming from a data file (for
simplicity, I just put the hard coded value. Same goes for the key, it
is also coming from a data file). I lost you in the compiler portion.

The code you posted used the "@" symbol so that you don't have to escape *
the backslash. *That's all I meant.

No matter where you get the text from, you need to escape special Regex *
characters, such as the backslash. *Try using the Regex.Escape() methodon *
your key string before passing it to the Regex class.

Pete
Thank you very much and the escape does the trick for me. I was
escaping both the source and key first. Iike you said, escape is only
needed for key.
Thanks again.
Jul 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.