regex in resx

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

Guest

When storing a regular expression in a resource file then extract using
"Resources.xxxx" all backslashes are now escaped. Is this normal behaviour
and if so how do I get back the actual value.
 
I believe Jon is right. String literals are stored in resource files as
strings. Backslashes are used in code to allow the compiler to identify
literal characters as opposed to tokens. There is no need for them in a
resource file. And one of the "new things" in Visual Studio 2005 that really
irritates me is that the debugger inserts escape characters into the strings
it shows. Occasionally, I have had the experiences of copying text from a
Watch, only to have to manually un-escape it in order to view it in NotePad,
where I can see it more easily. I wonder if this feature is something one
can turn off somehow...

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
Yep the debugger is indeed the problem in viewing the string, however, that
is not the answer it most certainly does not work using a resource directly
though I have now fixed the problem.

This does not work:
Regex.IsMatch( <string>, Resources.String1, RegexOptions.IgnoreCase )

But this does:
Regex.IsMatch( <string>, Regex.Unescape( Resources.String1 ),
RegexOptions.IgnoreCase )
 
KNG said:
Yep the debugger is indeed the problem in viewing the string, however, that
is not the answer it most certainly does not work using a resource directly
though I have now fixed the problem.

This does not work:
Regex.IsMatch( <string>, Resources.String1, RegexOptions.IgnoreCase )

But this does:
Regex.IsMatch( <string>, Regex.Unescape( Resources.String1 ),
RegexOptions.IgnoreCase )

That sounds odd.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
KNG said:
Example project attached.

Well, the resource starts with \\A\\b etc - so it won't match a string
which doesn't have "\A\b" in it (without either of those slashes
meaning anything interesting).

It shows up as "\\A\\b" etc even in the resource designer - I assume
you actually want it to be "\A\b" etc. How did you enter it in the
first place?
 
Ah, now I know what happened. Doh!

The program I use to create the regex does not know about adding the "@" at
the front of the string so escapes the string. Me being a bit worse for the
lack of sleep at present just copied the string into the resource, when I did
the example application I should have put two and two together to see the
error of my ways.

It all now fits together.

Many thanx for your help and sorry to have troubled you with what was really
a silly mistake.
 
KNG said:
Ah, now I know what happened. Doh!

The program I use to create the regex does not know about adding the "@" at
the front of the string so escapes the string. Me being a bit worse for the
lack of sleep at present just copied the string into the resource, when I did
the example application I should have put two and two together to see the
error of my ways.

It all now fits together.

Many thanx for your help and sorry to have troubled you with what was really
a silly mistake.

No problem at all - a lot of mistakes look silly when you know what
they are. Very few of them look silly when you're stuck on them :(
 
Back
Top