473,799 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex & \n

25 New Member
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 1224
balame2004
142 New Member
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 New Member
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 New Member
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
5893
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$", RegexOptions.IgnoreCase); That is, is the file CurrentOneFile.txt, CurrentTwoFile.txt, etc. shows in in the directory I'm watching, then I can take action.
4
4617
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 "&" to "&amp;" only within <comments> tag, so I need to build a Regex pattern to replace "&" only between <comments> and </comments>.
17
3982
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 http://forta.com/books/0672325667/
3
8261
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;}") Thanks, Craig
2
263
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 needs to skip escaped ampersands such as &amp; and  &+; will get the escaped ampersands (some inproper escapes will slip buy, but good enough for my purposes), but I need to replace all the ampersands that aren't escaped example:
7
3321
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 rest and use whats between the 2 Marker strings. I am not sure how to do this with Regex. Any help would be appreciated Thanks, Mike
0
771
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 sample: <Urls> <add key="/Shoes/-pg(\d+)\.aspx" value="/Shoes/Default.aspx?pg=$1"/> <add key="/Shoes/-t(\d+)\.aspx" value="/Shoes/Default.aspx?t=$1"/> <add key="/Shoes/-t(\d+)-pg(\d+)\.aspx" value="/Shoes/Default.aspx?t=$1&amp;pg=$2"/> <add...
4
337
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 33 33 33"); Sometimes my data has 10 sets of "22"'s , sometimes 12, 14 ,ect. I want the centervalue to start after ALL of the "22"'s but this is not happening. Once it see's the 10 sets it starts there, but I want it to start at
1
278
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 = new Regex(@"(+)", RegexOptions.Compiled); string op = re.Replace(FileToParse, "");
11
4945
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 guessing I need to use some sort of function in combination with regex. I am able to create a regex to find and replace the a href link very easily; however, using regex in combination with a find and replace in XSL i am extremly novice. I am wanting...
0
9688
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
9546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
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
10247
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
10031
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
9079
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
5467
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
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
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

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.