Change to upper case

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
MetroMunkee
Forum Newbie
Posts: 3
Joined: Thu Sep 13, 2007 5:05 pm

Change to upper case

Post by MetroMunkee »

Hi all,

I've searched long and hard through the forum, and can't find anything that addresses this question...forgive me if it's already been asked.

I'd like to use create a replace pattern that converts all letters captured in a referencing group to upper case. Is this possible?
From the Google searches I've done, I've found out about translation, but can't see how it's used unless there's some programming language like PERL or Python behind it.

I'm using a custom built application which is written in Java that has a regex utility in it, so the replace patterns utilize the $1, $2 back referencing notations.

The closest thing I've found from my research is something like:

Search pattern:
/w*

replace pattern:
$0 =~ tr/[a-z]/[A-Z]/;

Test string:
This 1 is a Test

I tried it, but to no avail.

Any help given would be much appreciated.

Thanks!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_replace_callback() would be the best course of action.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

you can't use strtoupper ? wasn't sure if you were using php in any of this or not..
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

Code: Select all

$string=strtr($string,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ");
is a little more flexible than strtoupper...

I wish strtoupper were smarter. It won't handle any accented characters. I usually end up just doing this:

Code: Select all

<style type="text/css">
.upcase { text-transform: uppercase; }
</style>
<span class="upcase">this should be uppercase</span>
Since that handles accented characters properly.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

You could use the phputf8 strtopupper function which takes accented characters into account.

From the top of my head, try this:

Code: Select all

preg_replace('/xxx([a-z]+)yyy/e', 'strtoupper(\'$1\')', $str);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As I've said before, avoid the e pattern modifier in preference to using preg_replace_callback().
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

mrkite wrote: I wish strtoupper were smarter. It won't handle any accented characters.
How "accented" characters are handled depends on your locale.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

feyd wrote:As I've said before, avoid the e pattern modifier in preference to using preg_replace_callback().
Okay, thanks for the tip. Apparently it's much faster indeed.
http://php.net/manual/en/function.preg- ... .php#65182
stereofrog wrote:
mrkite wrote: I wish strtoupper were smarter. It won't handle any accented characters.
How "accented" characters are handled depends on your locale.
Hmm, so strtoupper() actually can transform ä into Ä, as long as you use the correct locale? Didn't know that. Anyway, remember there's always mb_strtoupper() as well.
MetroMunkee
Forum Newbie
Posts: 3
Joined: Thu Sep 13, 2007 5:05 pm

Post by MetroMunkee »

Thanks for all your responses...unfortunately though, I'm not using PHP, so the strtoupper function won't help. What I'm doing is using RegexBuddy to test my search and replace patterns, and then inserting them into the application I'm using. I'm not using any programming language...the application simply has a regex functionality that you can put your s & r patterns into. Is there anything directly in Regex itself that will convert to upper case in the replace pattern?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

$s =~ tr/a-z/A-Z/;
There are 10 types of people in this world, those who understand binary and those who don't
MetroMunkee
Forum Newbie
Posts: 3
Joined: Thu Sep 13, 2007 5:05 pm

Post by MetroMunkee »

Thanks, I'm not using PERL or any programming language though.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

GeertDD wrote:Okay, thanks for the tip. Apparently it's much faster indeed.
http://php.net/manual/en/function.preg- ... .php#65182
My insistence is more about security issue avoidance than speed related, actually.
Post Reply