473,407 Members | 2,306 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,407 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 2572
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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,...
0
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...

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.