473,394 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,394 software developers and data experts.

Regex.Replace

Hi all

I use Regex.Replace to capitalize files name. Here is the code:

-----------------------------
static string CapitalizeText(string s)
{
return Regex.Replace(s, @"\w+", new
MatchEvaluator(CapitalizeText));
}

static string CapitalizeText(Match m)
{
string x = m.ToString();
if (char.IsLower(x[0]))
{
return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
}
return x;
}
-------------------------------------

if I call:

CapitalizeText("laura pausini - it's not goodbye.mp3")

I'll get "Laura Pausini - It'S Not Goodbye.Mp3", as you see, the uppercase
of 'S' in 'It's' is not so nice, how can I prevent this (by modify the
expression "\w+") ?

Thankyou
Nov 17 '05 #1
3 2571
Funny, last mesage I wrote I told someone to use Regex. And in this one
I'm telling you NOT to.

Replace both methods with:

static string CapitalizeText(string s)
{
System.Globalization.TextInfo myTI = new
System.Globalization.CultureInfo("en-US",false).TextInfo;
return myTI.ToTitleCase(s);
}

Now CapitalizeText("laura pausini - it's not goodbye.mp3")
returns: Laura Pausini - It's Not Goodbye.Mp3

You may want to move the myTI object to a class-level static.

"Omega" <om*****@gmx.net> wrote in message
news:k9********************************@4ax.com...
Hi all

I use Regex.Replace to capitalize files name. Here is the code:

-----------------------------
static string CapitalizeText(string s)
{
return Regex.Replace(s, @"\w+", new
MatchEvaluator(CapitalizeText));
}

static string CapitalizeText(Match m)
{
string x = m.ToString();
if (char.IsLower(x[0]))
{
return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
}
return x;
}
-------------------------------------

if I call:

CapitalizeText("laura pausini - it's not goodbye.mp3")

I'll get "Laura Pausini - It'S Not Goodbye.Mp3", as you see, the uppercase of 'S' in 'It's' is not so nice, how can I prevent this (by modify the
expression "\w+") ?

Thankyou

Nov 17 '05 #2
Great !

It works the way I wanted.

Thank you

On Wed, 2 Nov 2005 11:37:30 -0500, "James Curran" <ja*********@mvps.org>
wrote:
Funny, last mesage I wrote I told someone to use Regex. And in this one
I'm telling you NOT to.

Replace both methods with:

static string CapitalizeText(string s)
{
System.Globalization.TextInfo myTI = new
System.Globalization.CultureInfo("en-US",false).TextInfo;
return myTI.ToTitleCase(s);
}

Now CapitalizeText("laura pausini - it's not goodbye.mp3")
returns: Laura Pausini - It's Not Goodbye.Mp3

You may want to move the myTI object to a class-level static.

"Omega" <om*****@gmx.net> wrote in message
news:k9********************************@4ax.com...
Hi all

I use Regex.Replace to capitalize files name. Here is the code:

-----------------------------
static string CapitalizeText(string s)
{
return Regex.Replace(s, @"\w+", new
MatchEvaluator(CapitalizeText));
}

static string CapitalizeText(Match m)
{
string x = m.ToString();
if (char.IsLower(x[0]))
{
return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
}
return x;
}
-------------------------------------

if I call:

CapitalizeText("laura pausini - it's not goodbye.mp3")

I'll get "Laura Pausini - It'S Not Goodbye.Mp3", as you see, the

uppercase
of 'S' in 'It's' is not so nice, how can I prevent this (by modify the
expression "\w+") ?

Thankyou

Nov 17 '05 #3
How about using the System.Globalization.TextInfo class?

using System.Globalization;

static string CapitalizeText(string s)
{
TextInfo ti = new CultureInfo("en-US", false).TextInfo;
return ti.ToTitleCase(s);
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Omega" <om*****@gmx.net> wrote in message
news:k9********************************@4ax.com...
Hi all

I use Regex.Replace to capitalize files name. Here is the code:

-----------------------------
static string CapitalizeText(string s)
{
return Regex.Replace(s, @"\w+", new
MatchEvaluator(CapitalizeText));
}

static string CapitalizeText(Match m)
{
string x = m.ToString();
if (char.IsLower(x[0]))
{
return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
}
return x;
}
-------------------------------------

if I call:

CapitalizeText("laura pausini - it's not goodbye.mp3")

I'll get "Laura Pausini - It'S Not Goodbye.Mp3", as you see, the uppercase
of 'S' in 'It's' is not so nice, how can I prevent this (by modify the
expression "\w+") ?

Thankyou

Nov 17 '05 #4

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

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
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 ...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
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;}")...
9
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression)....
4
by: Morgan Cheng | last post by:
In my case, I have to remove any line containing "0.000000" from input string. In below case, it takes about 100 ms for 2k size input string. Regex.Replace(inputString, ".*0\\.000000.*\n", ""); I...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming...
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
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
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
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.