473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Regex & \n

25
Hi,
I want to replace "\n" with "!" in c#, i tried this:
Expand|Select|Wrap|Line Numbers
  1. String buffer=richTextBox.Text;
  2. buffer=buffer.Replace(@"\n", "!");
  3.  
and it worked, but this didnt:
Expand|Select|Wrap|Line Numbers
  1.  
  2. String buffer=richTextBox.Text;
  3. buffer = Regex.Replace(buffer, @"\n", "!");
  4.  
why?
Apr 9 '08 #1
3 1201
balame2004
142 100+
Hi,
I want to replace "\n" with "!" in c#, i tried this:
Expand|Select|Wrap|Line Numbers
  1. String buffer=richTextBox.Text;
  2. buffer=buffer.Replace(@"\n", "!");
  3.  
and it worked, but this didnt:
Expand|Select|Wrap|Line Numbers
  1.  
  2. String buffer=richTextBox.Text;
  3. buffer = Regex.Replace(buffer, @"\n", "!");
  4.  
why?
Hi,

Regex.Replace is working fine for me. Let me know with full source code so i can able to find out the issue.

Regards,
Balaji U
Apr 10 '08 #2
Svinja
25
Expand|Select|Wrap|Line Numbers
  1. String buffer=richTextBox.Text;
  2. buffer = Regex.Replace(buffer, @"\n", "!");
  3. //buffer=buffer.Replace(@"\n", "!");
  4. MessageBox.Show(buffer);
  5.  
there it is mate, as you can see i am geting text from richtextbox but if i put the text in the string manually like this:
Expand|Select|Wrap|Line Numbers
  1. buffer = Regex.Replace("a\nb", @"\n", "!");
then it works. I guess it has something to do with the fact that \n is single char and regex works only with strings, i really dont understand this.
Apr 10 '08 #3
Svinja
25
ok i figured it out, once again, the solution was hiding in the MSDN. It turns out that "\" is a special char for regex.replace so i cant use it. If i want to match "\" i must use "\\" like this:
Expand|Select|Wrap|Line Numbers
  1. String buffer=richTextBox.Text;
  2. buffer = Regex.Replace(buffer, @"\\n", "!");
  3. MessageBox.Show(buffer);
  4.  
but anyway thx for answering.
Apr 10 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Shmulik | last post by:
How can I (can I?) use a regular expression as the filter to a FileSystemWatcher? I want to watch for something like this: Regex regex = new Regex(@"^Current(One|Two|Three)File\.txt$",...
4
by: Hardy Wang | last post by:
Hi: I have a XML like <?xml version="1.0" ?> <object> <comments>www.site.com/page.aspx?param1=value1&param2=value2</comments> </object> Since "&" is invalid in XML, I need to replace all "&"...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
2
by: Stephen Brown | last post by:
I have a simple regex need and I've already wasted too much time on it spinning in circles. Can a regex god help a stranded soul? I just need to replace all non-escaped ampersands in a file. It...
7
by: AMP | last post by:
Hello, I have a long string that I need to take a section out of and use. The section I need always starts with "XX XX XX XX XX" and always ends with "YY YY YY YY YY" I want to throw away the...
0
by: Roshawn | last post by:
Hi, I'm doing url rewriting with the help of the Regex class. In my web.config file I have my rewrite rules specified. However, I have a lot of them as I have a dynamic storefront. Here's a...
4
by: AMP | last post by:
Hello, I have a Regex: Regex regex = new Regex(@"22 22 22 22 22 22 22 22 22 22(? <centervalue>.+)33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33...
1
by: AMP | last post by:
Hello, I am coming back to a project and I dont remember what the following Regex says I do know it removes all \r\n from the string, but I dont see how. Can someone explain this one? Regex re...
11
by: coflo | last post by:
Hello I would like to replace an a href link that is provided in the RSS below with my own link. The link that I am looking to replace is defined in the <description> tag within the RSS. Im...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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,...

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.