473,706 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Character Escapes Don't Work in VB Regex Replace?

Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text

For example, you'd think that thi

Regex.Replace(" <stuff>text</stuff>", "<stuff>", "<stuff>\n"

would give yo

<stuff
text</stuff

But it doesn't. Instead, you get

<stuff>\ntext </stuff

That's a totally arbitrary example. Escape characters just don't work at all in VB regex replacements- any of them. They work in C#, but that doesn't do me much good unless I start over

Thanks
Chri

Nov 20 '05 #1
6 8874

You can just append the actual characters in using Environment.New Line or
vbLf. VB does not use character escapes in strings like C#, instead you
just concatenate the string with the character, i.e.
Regex.Replace(" <stuff>text</stuff>", "<stuff>", "<stuff>" &
Environment.New Line)
-or-
Regex.Replace(" <stuff>text</stuff>", "<stuff>", "<stuff>" & vbLf)
Note that this is not specific to Regex.Replace; it is just that C# supports
character escapes in strings while VB does not.
Brian Davis
www.knowdotnet.com

"Chris Anderson" <ch************ @lionbridge.com > wrote in message
news:D0******** *************** ***********@mic rosoft.com...
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out
as literal text.
For example, you'd think that this

Regex.Replace(" <stuff>text</stuff>", "<stuff>", "<stuff>\n" )

would give you

<stuff>
text</stuff>

But it doesn't. Instead, you get.

<stuff>\ntext </stuff>

That's a totally arbitrary example. Escape characters just don't work at all in VB regex replacements- any of them. They work in C#, but that doesn't
do me much good unless I start over.
Thanks,
Chris

Nov 20 '05 #2
I guess I should have pointed that out in the original posting..

I could certainly do it like that or by a few other means in the code. But the problem is that I'm loading replace expressions dynamically from a text file and I need the ability (specifically) to allow Unicode characters such as \u200e in the replace expression, and also the whole range of general character escapes. I can't hard code those. I could come up with a little parser to replace character escapes from the script before sending them to the regex replace, but that's a pain. There's no reason that VB shouldn't support this. The regex engine is part of the .net framework and should conform to common functionality, as far as I'm concerned. Anyway, regardless of the fact that VB doesn't generally support escape characters doesn't mean that they shouldn't work in regex. Afterall, $1 means nothing special in VB, but does in a regex replace string

Now I'm just ranting... Point being, I need to do this the way it should work. I'm hoping there's either some sort of update available or some crazy secrect syntax such as $#\\u002e to do what I need

Thanks.
Nov 20 '05 #3
Chris,
As Brian suggested:

VB.NET just does not support C# escape sequences, nor does VB.NET define its
own escape sequences!
The "\n" is only supported in regular expressions and replacement patterns,
not the replace string according to the Character Escapes section of Regular
Expression Language Elements:

http://msdn.microsoft.com/library/de...geElements.asp

This is not a VB.NET problem per se, C# & every other .NET language will
have the same problem, as the RegEx class itself is defining this behavior.
(Read as I hope your rant is against the RegEx class and not VB.NET! ;-))

Unfortunately I do not know of a predefined routine that will replace the C#
escape sequences with their respective characters. If you build one, I would
recommend using a StringBuilder in the implementation.

Hope this helps
Jay

"Chris Anderson" <an*******@disc ussions.microso ft.com> wrote in message
news:7E******** *************** ***********@mic rosoft.com...
I guess I should have pointed that out in the original posting...

I could certainly do it like that or by a few other means in the code. But the problem is that I'm loading replace expressions dynamically from a text
file and I need the ability (specifically) to allow Unicode characters such
as \u200e in the replace expression, and also the whole range of general
character escapes. I can't hard code those. I could come up with a little
parser to replace character escapes from the script before sending them to
the regex replace, but that's a pain. There's no reason that VB shouldn't
support this. The regex engine is part of the .net framework and should
conform to common functionality, as far as I'm concerned. Anyway, regardless
of the fact that VB doesn't generally support escape characters doesn't mean
that they shouldn't work in regex. Afterall, $1 means nothing special in VB,
but does in a regex replace string.
Now I'm just ranting... Point being, I need to do this the way it should work. I'm hoping there's either some sort of update available or some crazy
secrect syntax such as $#\\u002e to do what I need.
Thanks.

Nov 20 '05 #4
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/de...cterescapes.as

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.
Nov 20 '05 #5
Chris,
Correct, the "replacemen t pattern" which is the second argument to
Regex.Replace (in the sample you gave) supports \n, which is the pattern to
match.

However! The "replacemen t", 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/de...laceTopic6.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.Reg ularExpressions .Regex.Replace( @"<stuff>tex t</stuff>",
@"<stuff>", @"<stuff>\n" );
System.Diagnost ics.Debug.Write Line(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" <an*******@disc ussions.microso ft.com> wrote in message
news:2A******** *************** ***********@mic rosoft.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/de...terescapes.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.
Nov 20 '05 #6
Chris,
If you are still following this thread...

While searching for something else, I just came across RegEx.Escape and
RegEx.Unescape that will escape & unescape strings for you (including
whitespace). It may help in your efforts.

http://msdn.microsoft.com/library/de...scapeTopic.asp

http://msdn.microsoft.com/library/de...scapeTopic.asp

Hope this helps
Jay
"Chris Anderson" <an*******@disc ussions.microso ft.com> wrote in message
news:2A******** *************** ***********@mic rosoft.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/de...terescapes.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.
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
27889
by: los | last post by:
Hi, I've created a web application using struts. I am trying to solve an issue where in one of the forms where I want to allow the values inserted to be special characters from other languages, but not symbols such as (, <, +, }, etc... Creating the regular expression that handles these values is becoming quite hard to find. Right now I have ^()*$ and this works for some utf-8 characters such as ã, é, ó, etc... But doesn't work for...
1
12648
by: GlennH | last post by:
I am having trouble removing a pipe character using Regex.Replace - see the 2 NUnit tests below: The first replace works fine and the second Replace does not work. I've tried escaping the pipe character. Can anyone get this to work? Thanks
3
5895
by: gogaz | last post by:
Hi there, I have a small problem. I need to replace the "?" character from user input string to "-". I have tried: Regex.Replace(sStr,"\?","-"); //GIVES ERROR Regex.Replace(sStr,@"?","-"); //GIVES ERROR Regex.Replace(sStr,@"\?","-"); //DOES NOT WORKS AS IT LOOKS FOR "\?" Please help!
15
18317
by: pkaeowic | last post by:
I am having a problem with the "escape" character \e. This code is in my Windows form KeyPress event. The compiler gives me "unrecognized escape sequence" even though this is documented in MSDN. Any idea if this is a bug? if (e.KeyChar == '\e') { this.Close(); }
24
4839
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross multiple lines. It would appear though that every distinct set of data starts with a 'code' that is always the 25 characters long. The text is variable however. Assuming i've read the contents of the file into the string myfile, how do i split my...
2
1924
by: Mo | last post by:
Hi, What is the regex to replace & # 1 0 6 ; to character? Any Ideas? Thanks, Mo
6
1885
by: Academia | last post by:
I want to search for Dim and replace it with Dim That is, I want to change the first character of Dim variable names to upper case. I can't figure know to use Regular Expression to do that. Is it possible?
2
1865
by: christopher taylor | last post by:
hello python-list! the other day, i was trying to match unicode character sequences that looked like this: \\uAD0X... my issue, is that the pattern i used was returning:
5
2578
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @ shouldn't be replaced.
0
8781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9156
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9055
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7911
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4709
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2502
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.