Re: Character Escapes Don't Work in VB Regex Replace?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 02/26/04


Date: Wed, 25 Feb 2004 19:03:22 -0600

Chris,
Correct, the "replacement pattern" which is the second argument to
Regex.Replace (in the sample you gave) supports \n, which is the pattern to
match.

However! The "replacement", which is the third argument to Regex.Replace,
which is your argument with \n in it, is not listed on the page you gave.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemTextRegularExpressionsRegexClassReplaceTopic6.asp

>> Using escape characters in C# replacement patterns
>> does work as it says in the documentation, and as
>> in every other language or application that implements regex.
No! They do not work!! :-| What documentation? (not the page you gave!)

They do not work in that RegEx will not honor them, however C# itself may.
Try the following (in C#):

    string s =
System.Text.RegularExpressions.Regex.Replace(@"<stuff>text</stuff>",
@"<stuff>", @"<stuff>\n");
    System.Diagnostics.Debug.WriteLine(s);

Where I am telling C# not to replace C#'s escape sequences.

Notice that the result still contains the \n, as Regex does not modify the
\n in the replacement text.

Hope this helps
Jay

"Chris Anderson" <anonymous@discussions.microsoft.com> wrote in message
news:2AC493A6-E389-44EF-A482-9590787248D7@microsoft.com...
> Well, I figured I was hoping too much. I'd still call it a bug, though. It
actually says in the documentation for regex that all character escapes are
supported both in regular expressions and replacement patterns.
>
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterescapes.asp
>
> Unfortunately, it's only half true in VB. I only mention C# because I
wanted to see if this was a VB specific issue, or the .net regex engine.
Using escape characters in C# replacement patterns does work as it says in
the documentation, and as in every other language or application that
implements regex. I've been using VB for 6 years, and think .net is great,
but it's very disappointing and confusing that VB does implement the regex
behavior in a standard way. I'll probably end up writing my own fix, and
share it with anyone else who is frustrated by this.



Relevant Pages

  • Re: Character Escapes Dont Work in VB Regex Replace?
    ... RegEx.Unescape that will escape & unescape strings for you (including ... supported both in regular expressions and replacement patterns. ... wanted to see if this was a VB specific issue, or the .net regex engine. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Regular Expression reserved letters
    ... The problem ist, that e.g.),,are reserved words in regex. ... them in filter expressions i have to escape them with \). ... If, on the other hand, you have a portion of an expresion that must be ... documentation here: <http://java.sun.com/j2se/1.4.2/docs/api/java/util/ ...
    (comp.lang.java.programmer)