473,624 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Regex Replace() question.

I'm trying to replace \n with @(representing the actual new line
character). Of course, \\n should not be replaced. What I'm trying to
do is simply what the good-old printf does.
For example, "\\n \n" should be converted to "\\n @". I used,
Regex r = new Regex(@"[^\\](\\n)");
textBox2.Text = r.Replace(textB ox1.Text, "@");
But the result was "\\n@". It looks like one character before \n is
also treated as a character to be replaced. How can I fix this? Again,
the correct result is "\\n @".
Thank you.
Dec 23 '07 #1
2 1629

"Sin Jeong-hun" <ty*******@gmai l.comwrote in message
news:66******** *************** ***********@e10 g2000prf.google groups.com...
I'm trying to replace \n with @(representing the actual new line
character). Of course, \\n should not be replaced. What I'm trying to
do is simply what the good-old printf does.
For example, "\\n \n" should be converted to "\\n @". I used,
Regex r = new Regex(@"[^\\](\\n)");
textBox2.Text = r.Replace(textB ox1.Text, "@");
But the result was "\\n@". It looks like one character before \n is
also treated as a character to be replaced. How can I fix this? Again,
the correct result is "\\n @".

The space in the input string is the character that matches [^\\] in the
pattern and is therefore part of the string which gets replaced.

Try:-

Regex r = new Regex(@"(?<!\\) \\n");

See:-

http://msdn2.microsoft.com/en-us/library/bs2twtah.aspx

--
Anthony Jones - MVP ASP/ASP.NET
Dec 23 '07 #2
On Dec 23, 10:51*pm, "Anthony Jones" <A...@yadayaday ada.comwrote:
"Sin Jeong-hun" <typing...@gmai l.comwrote in message

news:66******** *************** ***********@e10 g2000prf.google groups.com...
I'm trying to replace \n with @(representing the actual new line
character). Of course, \\n should not be replaced. What I'm trying to
do is simply what the good-old printf does.
For example, "\\n \n" should be converted to "\\n @". I used,
Regex r = new Regex(@"[^\\](\\n)");
textBox2.Text = r.Replace(textB ox1.Text, "@");
But the result was "\\n@". It looks like one character before \n is
also treated as a character to be replaced. How can I fix this? Again,
the correct result is "\\n @".

The space in the input string is the character that matches [^\\] in the
pattern and is therefore part of the string which gets replaced.

Try:-

Regex r = new Regex(@"(?<!\\) \\n");

See:-

http://msdn2.microsoft.com/en-us/library/bs2twtah.aspx

--
Anthony Jones - MVP ASP/ASP.NET
Thanks a lot! Worked like a charm. And thanks for the link, too.
Dec 23 '07 #3

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

Similar topics

16
9060
by: Stephane | last post by:
Hi, I'm trying to replace parenthesis using Regex.replace but I'm always having this error: System.ArgumentException: parsing ":-)" - Too many )'s. Parameter name: :-) Here's my code: Regex.Replace(input,":-)","img",RegexOptions.Compiled |
3
55379
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me how to do this using the string.Replace function? Thanks!
1
1944
by: Theo Chakkapark | last post by:
I'm having issues trying to replace text with PHP. For example, if I have a string of text that reads: {tag} And want to replace that with: $_POST
2
1448
by: K. Shier | last post by:
my regex for matching phone #'s: \({0,1}(?<areacode>{3})\){0,1}( |-){0,1}(?<prefix>{3})( |-){0,1}(?<suffix>{4}) which i then .Replace to become: (${areacode}) ${prefix}-${suffix} question: how do i fix the match regex so that it 'stops' after the suffix of the phone # is matched? right now, it will return '(800) 555-1212345' if i type in '8005551212345' - i just want '(800) 555-1212'.
9
4243
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression). Why does this function replace the found reg ex. with the actual string "\t" and not a tab? (in the example below out of frustration I actually hardcoded the "\t") Private Shared Function replaceAll(ByVal strIn As String, ByVal strFind As...
6
5880
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead". This parrot is sleeping. Really, it is sleeping. to This parrot is dead. Really, it is dead.
3
1239
by: P2000 | last post by:
I want to make a regex for the replace function that will replace any "a" with "A" and any "b" with "B". This is what I did so far... (I'm trying to learn this RegEx thing) <form> <input name="source" onkeyup="target.value=s.value.replace(/a/g, 'A')"/><br /> <input name="target" /> </form>
15
50213
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
1728
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming operation by a long-shot is the regex.Replace. Basically the only purpose of it is to remove spaces between opening/closing tags and the element name. Surely there is a better way. private string FixupJavascript(string htmlCode) { string result...
0
8236
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
8679
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
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...
0
7159
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
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
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...
1
2606
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
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.