473,473 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2574
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
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...
1
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...
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.