473,406 Members | 2,217 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,406 software developers and data experts.

regex help

I need to write a regular exp to replace more than 1 space to a single space
input:
this is a computer program

output
this is a computer program

output = Regex.Replace(input, "something here", " ")
thanks

Howard
Apr 8 '06 #1
17 2740
KH
@"\s+"
"Howard" wrote:
I need to write a regular exp to replace more than 1 space to a single space
input:
this is a computer program

output
this is a computer program

output = Regex.Replace(input, "something here", " ")
thanks

Howard

Apr 8 '06 #2
That will match *any* white space character. To replace only spaces:

[ ]+

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"KH" <KH@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
@"\s+"
"Howard" wrote:
I need to write a regular exp to replace more than 1 space to a single
space
input:
this is a computer program

output
this is a computer program

output = Regex.Replace(input, "something here", " ")
thanks

Howard

Apr 8 '06 #3
This will do it.

output = Regex.Replace("this is a computer program", "[ ]{2,}", " ");
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Ol**************@TK2MSFTNGP02.phx.gbl...
That will match *any* white space character. To replace only spaces:

[ ]+

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"KH" <KH@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
@"\s+"
"Howard" wrote:
I need to write a regular exp to replace more than 1 space to a single
space
input:
this is a computer program

output
this is a computer program

output = Regex.Replace(input, "something here", " ")
thanks

Howard


Apr 9 '06 #4
How about this?

string str = "this is a computer program";
for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));

Joking aside, I ran a quick (and very unscientific) benchmark to
compare this against the regular expression method, and it's quicker
for small strings with only a few multiple spaces. Might be worth
rewriting as a while loop if you're actually going to use it though
:¬)

Further to Rocky's reply, the square brackets are optional, so you
could have just:
" {2,}"

Personally, I think the brackets make it more readable, so I'd keep
them in.

Chris

Apr 9 '06 #5
On Sat, 8 Apr 2006 14:51:51 -0700, "Howard" <ho*******@yahoo.com> wrote:
I need to write a regular exp to replace more than 1 space to a single space
input:
this is a computer program

output
this is a computer program

output = Regex.Replace(input, "something here", " ")
thanks

Howard


The other folks have already answered your question. Below is a link to a tool
that can help you design regular expressions. Since I am no regular expressions
expert, I use it all the time ;o)

http://www.gotdotnet.com/Community/U...1-4ee2729d7322

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Apr 9 '06 #6
Chris Fulstow <ch**********@hotmail.com> wrote:
How about this?

string str = "this is a computer program";
for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));

Joking aside, I ran a quick (and very unscientific) benchmark to
compare this against the regular expression method, and it's quicker
for small strings with only a few multiple spaces. Might be worth
rewriting as a while loop if you're actually going to use it though
:¬)


I would certainly have written it as a while loop. However, you should
be aware that the above can go into an infinite loop. Scary as it
sounds, if there's a "zero-width non-joiner character" between two
spaces, IndexOf will find it but Replace won't. Try running the above
on:

"hello \u200c there"

Scary, eh?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #7
"Chris Fulstow" <ch**********@hotmail.com> wrote:
string str = "this is a computer program";
for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
Joking aside, I ran a quick (and very unscientific) benchmark to
compare this against the regular expression method

I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian
Apr 10 '06 #8
> I'd go straight for this !! ... (untested c++)

I didn't know this was C++ newsgroup.
"Lucian Wischik" <lu***@wischik.com> wrote in message
news:3d********************************@4ax.com...
"Chris Fulstow" <ch**********@hotmail.com> wrote:
string str = "this is a computer program";
for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
Joking aside, I ran a quick (and very unscientific) benchmark to
compare this against the regular expression method

I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian

Apr 10 '06 #9
Rocky schreef:
I'd go straight for this !! ... (untested c++)

I didn't know this was C++ newsgroup.


That's ok... I wouldn't call that stuff below C++.


"Lucian Wischik" <lu***@wischik.com> wrote in message
news:3d********************************@4ax.com...
"Chris Fulstow" <ch**********@hotmail.com> wrote:
string str = "this is a computer program";
for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
Joking aside, I ran a quick (and very unscientific) benchmark to
compare this against the regular expression method

I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian


Apr 10 '06 #10
Try it in C# and see what you get smart guy. Besides, the code doesn't even
work in C++ (until I fixed it).

"Frank van Dijk" <f.******@home.nl> wrote in message
news:e1**********@news1.zwoll1.ov.home.nl...
Rocky schreef:
I'd go straight for this !! ... (untested c++)

I didn't know this was C++ newsgroup.


That's ok... I wouldn't call that stuff below C++.


"Lucian Wischik" <lu***@wischik.com> wrote in message
news:3d********************************@4ax.com...
"Chris Fulstow" <ch**********@hotmail.com> wrote:

string str = "this is a computer program";
for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
Joking aside, I ran a quick (and very unscientific) benchmark to
compare this against the regular expression method
I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian



Apr 10 '06 #11
Just for giggles, here is the code in C#. Report any errors to the
authorities immediately.
public char[] EnsureSingleSpacing(string str)
{

unsafe
{

string src = str;
char[] dst = new char[src.Length - 1];

fixed (char* p = src)
fixed (char* p1 = dst)
{
char* temp = p;
char* temp2 = p1;
char prev = ' ';
while (*temp != 0)
{

if (*temp == ' ' && prev == ' ' ) temp++;

else
{
if (*temp !='\n')prev = *temp;
*temp2++ = *temp++;
}

}
*temp2++ = '\0';

return dst;

}

}

}

"Frank van Dijk" <f.******@home.nl> wrote in message
news:e1**********@news1.zwoll1.ov.home.nl...
Rocky schreef:
I'd go straight for this !! ... (untested c++)

I didn't know this was C++ newsgroup.


That's ok... I wouldn't call that stuff below C++.


"Lucian Wischik" <lu***@wischik.com> wrote in message
news:3d********************************@4ax.com...
"Chris Fulstow" <ch**********@hotmail.com> wrote:

string str = "this is a computer program";
for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
Joking aside, I ran a quick (and very unscientific) benchmark to
compare this against the regular expression method
I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian



Apr 10 '06 #12
Rocky schreef:
Try it in C# and see what you get smart guy. Besides, the code doesn't even
work in C++ (until I fixed it).
My point being that while it might pass a C++ compiler it's actually C
with the accidental use of a few C++ keywords. The bugs (memory leak,
string length, string termination) perhaps arising from the C-ness.

Lucian Wischik probably posted his snippet as an ironic comment anyway,
so...

..

"Frank van Dijk" <f.******@home.nl> wrote in message
news:e1**********@news1.zwoll1.ov.home.nl...
Rocky schreef:
I'd go straight for this !! ... (untested c++)
I didn't know this was C++ newsgroup.


That's ok... I wouldn't call that stuff below C++.


"Lucian Wischik" <lu***@wischik.com> wrote in message
news:3d********************************@4ax.com ...
"Chris Fulstow" <ch**********@hotmail.com> wrote:
>string str = "this is a computer program";
>for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
>Joking aside, I ran a quick (and very unscientific) benchmark to
>compare this against the regular expression method
I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian

Apr 10 '06 #13

"Rocky" <no*****@nowhere.com> wrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...
Just for giggles, here is the code in C#. Report any errors to the
authorities immediately.
public char[] EnsureSingleSpacing(string str)
{

unsafe
{

string src = str;
char[] dst = new char[src.Length - 1];

fixed (char* p = src)
fixed (char* p1 = dst)
{
char* temp = p;
char* temp2 = p1;
char prev = ' ';
while (*temp != 0)
{

if (*temp == ' ' && prev == ' ' ) temp++;

else
{
if (*temp !='\n')prev = *temp;
*temp2++ = *temp++;
}

}
*temp2++ = '\0';

return dst;

}

}

}

"Frank van Dijk" <f.******@home.nl> wrote in message
news:e1**********@news1.zwoll1.ov.home.nl...
Rocky schreef:
I'd go straight for this !! ... (untested c++)
I didn't know this was C++ newsgroup.


That's ok... I wouldn't call that stuff below C++.


"Lucian Wischik" <lu***@wischik.com> wrote in message
news:3d********************************@4ax.com...

"Chris Fulstow" <ch**********@hotmail.com> wrote:

>string str = "this is a computer program";
>for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
>Joking aside, I ran a quick (and very unscientific) benchmark to
>compare this against the regular expression method
I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian

Apr 10 '06 #14
I think it would be better to return a string instead of a char[]. Don't you
think?
// Updated code
public string EnsureSingleSpacing(string str)
{

// Other Code Omitted
return new string(p1);

}

"Rocky" <no*****@nowhere.com> wrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...
Just for giggles, here is the code in C#. Report any errors to the
authorities immediately.
public char[] EnsureSingleSpacing(string str)
{

unsafe
{

string src = str;
char[] dst = new char[src.Length - 1];

fixed (char* p = src)
fixed (char* p1 = dst)
{
char* temp = p;
char* temp2 = p1;
char prev = ' ';
while (*temp != 0)
{

if (*temp == ' ' && prev == ' ' ) temp++;

else
{
if (*temp !='\n')prev = *temp;
*temp2++ = *temp++;
}

}
*temp2++ = '\0';

return dst;

}

}

}

"Frank van Dijk" <f.******@home.nl> wrote in message
news:e1**********@news1.zwoll1.ov.home.nl...
Rocky schreef:
I'd go straight for this !! ... (untested c++)
I didn't know this was C++ newsgroup.


That's ok... I wouldn't call that stuff below C++.


"Lucian Wischik" <lu***@wischik.com> wrote in message
news:3d********************************@4ax.com...

"Chris Fulstow" <ch**********@hotmail.com> wrote:

>string str = "this is a computer program";
>for (;str.IndexOf(" ") > -1; str=str.Replace(" ", " "));
>Joking aside, I ran a quick (and very unscientific) benchmark to
>compare this against the regular expression method
I'd go straight for this !! ... (untested c++)

const char *src="this is a computer program";
char *dst = new char[strlen(src)];
bool space=false;

while (*src!=0)
{ if (*src==' ' && space) src++;
else {space=(*src==' '); *dst++=*src++;}
}

--
Lucian

Apr 10 '06 #15
Rocky schreef:
I think it would be better to return a string instead of a char[]. Don't you
think?
yes


return new string(p1);


return new string(dst) eliminates the need for a terminating 0.
Just for giggles, here is the code in C#. Report any errors to the
authorities immediately.
dear authorities,


public char[] EnsureSingleSpacing(string str)
{

unsafe
{

string src = str;
redundant.
char[] dst = new char[src.Length - 1];
src.Length+1 if you use a terminating 0, src.Length otherwise.

fixed (char* p = src)
fixed (char* p1 = dst)
{
char* temp = p;
char* temp2 = p1;
char prev = ' ';
this causes a space to be blown away if it's the first character in the
string.
while (*temp != 0)
{

if (*temp == ' ' && prev == ' ' ) temp++;

else
{
if (*temp !='\n')prev = *temp;
huh ? what is special about newline ?
*temp2++ = *temp++;
}

}
*temp2++ = '\0';
only needed if using p1 to construct your string. dst is a counted array.

return dst;

}

}

}

Apr 10 '06 #16
>> src.Length + 1 if you use a terminating 0 <<
Yep, I was thinking about VB at the time
return new string(dst) eliminates the need for a terminating 0 << Nope. Did you try this? It returns the FULL array length (except for a
string with NO spaces)

Try this:
string result = EnsureSingleSpacing(" this is a computer
program ");
Console.WriteLine(result.Length);
It will return 48 instead of 27.

Then this:
string result = EnsureSingleSpacing("ALineWithNoSpaces");
Console.WriteLine(result.Length);
It will return 17, the exact number.
this causes a space to be blown away if it's the first character in the
string <<
Yes, it does. That's the way I wanted it. No spaces at the beginning of each
line.
huh ? what is special about newline ? > You could get too many spaces. Everything is SINGLE spaced. See above.

I really appreciate you critiquing my code. It makes me feel special. Maybe
you could show me some of your code so I could repay the favor?

"Frank van Dijk" <f.******@home.nl> wrote in message
news:e1**********@news4.zwoll1.ov.home.nl... Rocky schreef:
I think it would be better to return a string instead of a char[]. Don't
you think?


yes


return new string(p1);


return new string(dst) eliminates the need for a terminating 0.
Just for giggles, here is the code in C#. Report any errors to the
authorities immediately.
dear authorities,
public char[] EnsureSingleSpacing(string str)
{

unsafe
{

string src = str;
redundant.
char[] dst = new char[src.Length - 1];
src.Length+1 if you use a terminating 0, src.Length otherwise.

fixed (char* p = src)
fixed (char* p1 = dst)
{
char* temp = p;
char* temp2 = p1;
char prev = ' ';
this causes a space to be blown away if it's the first character in the
string.
while (*temp != 0)
{

if (*temp == ' ' && prev == ' ' ) temp++;

else
{
if (*temp !='\n')prev = *temp;
huh ? what is special about newline ?
*temp2++ = *temp++;
}

}
*temp2++ = '\0';
only needed if using p1 to construct your string. dst is a counted array.

return dst;

}

}

}

Apr 11 '06 #17
Rocky schreef:
src.Length + 1 if you use a terminating 0 <<
Yep, I was thinking about VB at the time

return new string(dst) eliminates the need for a terminating 0 <<
Nope. Did you try this? It returns the FULL array length (except for a
string with NO spaces)
You're right. Should be something like new string(dst,0,temp2-p1). Which
makes it slightly less attractive.

this causes a space to be blown away if it's the first character in the
string <<


Yes, it does. That's the way I wanted it. No spaces at the beginning of each
line.
huh ? what is special about newline ? >


You could get too many spaces. Everything is SINGLE spaced. See above.


Oh, I foolishly interpreted "here is the code in C#" as implying that
like the other samples in this thread, "the code" was intended to solve
the OP's problem.

I really appreciate you critiquing my code. It makes me feel special.
Anything to oblige.
Maybe you could show me some of your code so I could repay the favor?


Sure. To stay on topic and because I love the 'closure' feature of C#,
here's a silly solution to the OP's problem using an anonymous method.
In real life I'd just use the regexp substitution though. And yes, I am
aware of the existence of the foreach keyword.

static string JustOneSpace(string s) {
StringBuilder result=new StringBuilder();
bool wasSpace=false;
Array.ForEach(s.ToCharArray(), delegate(char c) {
if (!wasSpace || c!=' ') {
result.Append(c);
}
wasSpace = c == ' '; });
return result.ToString();
}

Here's a rough equivalent in C++. I hope no-one objects to me posting it
in this newsgroup :-) In real life I'd use a regexp subst here too.

bool is_both_space(const char c1,const char c2) {
return c1==' ' && c2==' ';
}

string just_one_space(const string& s) {
string result;
unique_copy(s.begin(),s.end(),inserter(result,resu lt.begin()),
is_both_space);
return result;
}
Apr 11 '06 #18

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

Similar topics

6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
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 ...
7
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
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...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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
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...

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.