473,386 Members | 1,748 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,386 software developers and data experts.

Regex: replacing \n and spaces

Hi everyone,
I'm having some problems using Regex; I have a long string that is
delimetered in a random fashion by a combination of spaces and \n's for
newlines. I have five possiblities:

// Character can be a-zA-Z0-9 plus any non-alphanumeric character (. ,
\ [ ] etc.)
// five possibilities:
// 1: Characters \nCharacters -Characters Characters
// 2: Characters\n Characters -Characters Characters
// 3: Characters \nCharacters -Characters Characters
// 4: Characters \n Characters -Characters Characters
// 5: Characters\nCharacters -Characters Characters

So that a string like
A B \nC D\n E\nF \n G\nH
becomes
A B C D E F G H

Would regex be the "right" method to use, or some of the C# String
methods, like replace(...)?
If the former, could someone give me some helpful advice? It looks,
like for case 1, the regex
string would be something like .*\s\n.* but in this case, isn't \n
regarded as whitespace, like \s ?

TIA

Trev

Jan 5 '07 #1
4 2297
Try this:

static string removeSpaceNewLine(string inputString)
{
return Regex.Replace(inputString, @"[\s]{1,}", " ");
}

I've tested with all your samples. Works fine.

string s1 = removeSpaceNewLine("Characters \nCharacters");
string s2 = removeSpaceNewLine("Characters\n Characters");
string s3 = removeSpaceNewLine("Characters \nCharacters");
string s4 = removeSpaceNewLine("Characters \n Characters");
string s5 = removeSpaceNewLine("Characters\nCharacters");
string s6 = removeSpaceNewLine("A B \nC D\n E\nF \n G\nH");

Davey

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Beer is part of the life.
http://www.lovebeers.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Trev wrote:
Hi everyone,
I'm having some problems using Regex; I have a long string that is
delimetered in a random fashion by a combination of spaces and \n's for
newlines. I have five possiblities:

// Character can be a-zA-Z0-9 plus any non-alphanumeric character (. ,
\ [ ] etc.)
// five possibilities:
// 1: Characters \nCharacters -Characters Characters
// 2: Characters\n Characters -Characters Characters
// 3: Characters \nCharacters -Characters Characters
// 4: Characters \n Characters -Characters Characters
// 5: Characters\nCharacters -Characters Characters

So that a string like
A B \nC D\n E\nF \n G\nH
becomes
A B C D E F G H

Would regex be the "right" method to use, or some of the C# String
methods, like replace(...)?
If the former, could someone give me some helpful advice? It looks,
like for case 1, the regex
string would be something like .*\s\n.* but in this case, isn't \n
regarded as whitespace, like \s ?

TIA

Trev
Jan 5 '07 #2
Hello Davey,

I believe you that your solution works, so I haven't even checked with the
OP :-) I just wanted to add that the expression you show

[\s]{1,}

seems rather complicated to me - it should be absolutely equal to

\s+

which is a lot simpler.
Oliver Sturm
--
http://www.sturmnet.org/blog
Jan 5 '07 #3
Thanks Oliver, you are absolutely right. I am new to Regular Express.
It is just so confusing.

Davey.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Beer is part of the life.
http://www.lovebeers.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Oliver Sturm wrote:
Hello Davey,

I believe you that your solution works, so I haven't even checked with the
OP :-) I just wanted to add that the expression you show

[\s]{1,}

seems rather complicated to me - it should be absolutely equal to

\s+

which is a lot simpler.
Oliver Sturm
--
http://www.sturmnet.org/blog
Jan 5 '07 #4
Thanks! Works great!
>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Beer is part of the life.
http://www.lovebeers.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nice sig by the way ;)

Jan 11 '07 #5

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

Similar topics

4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
7
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
5
by: Greg Collins [InfoPath MVP] | last post by:
I couldn't find anything in my searches... I'm wondering if there's a Regex (with or without additional C# code) that can convert a either "lowerCamelCase" or "UpperCamelCase" into a proper "Title...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&") Regex.Replace(Subject, "\'", "'") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&|'}")...
4
by: Jéjé | last post by:
Hi, I have a file which contain 1 pair of values by line like: Name1=Value1 = I nned to store these pair of values in a sortedlist. So the result expected for the 2 samples lines is: Key ...
2
by: Craig Buchanan | last post by:
I have a HTML fragment that looks like this: <tr> <td valign="top" nowrap><span class="textBold">Property ID: </span></td> <td valign="top" nowrap colspan="4"...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
5
by: Bragadiru | last post by:
Hi, I'm using the following Regex to parse for method parameters. It works if there are no spaces between commas. How can I change the regex to support method calls like : MyMethod('uno', ...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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...

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.