Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression help 1

Status
Not open for further replies.

shades44

Programmer
Jun 14, 2004
25
CA
Hi,

can anyone give me the regular expression for finding sole instances of "\n", not grouped in twos or more.. something along the lines of:

{not "\n"}{"\n"}{not "\n"}

Thank you,
shady
 
You are on the right track. ^ is not in regular expressions. You also want to make sure you aren't limiting other characters, so put the ^\n's in a range box []. Here's a quick example:
Code:
using System.Text.RegularExpressions;
...
string pattern1 = "This is \n\n a test.";
string pattern2 = "This is \n pattern 2.";
Regex test_pattern=new Regex("[^\n]\n[^\n]");
if(test_pattern.IsMatch(pattern1))
  Console.WriteLine("Pattern 1 matches.");
if(test_pattern.IsMatch(pattern2))
  Console.WriteLine("Pattern 2 matches.");
 
Thank you very much oppcos. You've been a great help!

shady
 
There was something i forgot to mention before..

string result = Regex.Replace("cat\n monkey \n\n mouse", "[^\\n]\\n[^\\n]", " ");

'result' in this case will be: "ca monkey \n\n mouse" because the letters being replaced are "t\n " ie. the \n and the surrounding letters. How do i only find one occurance of \n and then replace it only.. so that when i come to a string like "cat\n monkey \n\n mouse" i end up with "cat monkey \n\n mouse", not "ca monkey \n\n mouse".

Any ideas?
 
I discovered how to do it.. for anyone interested i did it using groups:

Regex.Replace(myString, "([^\\n])(\\n)([^\\n])", "$1 $3");

this will find every sole occurence of \n and replace it with a space.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top