Page 1 of 1

Change to upper case

Posted: Thu Sep 13, 2007 5:23 pm
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!!

Posted: Thu Sep 13, 2007 5:24 pm
by feyd
preg_replace_callback() would be the best course of action.

Posted: Thu Sep 13, 2007 5:27 pm
by infolock
you can't use strtoupper ? wasn't sure if you were using php in any of this or not..

Posted: Thu Sep 13, 2007 5:39 pm
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.

Posted: Thu Sep 13, 2007 11:32 pm
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);

Posted: Thu Sep 13, 2007 11:38 pm
by feyd
As I've said before, avoid the e pattern modifier in preference to using preg_replace_callback().

Posted: Fri Sep 14, 2007 4:43 am
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.

Posted: Fri Sep 14, 2007 9:37 am
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.

Posted: Fri Sep 14, 2007 10:45 am
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?

Posted: Fri Sep 14, 2007 10:51 am
by VladSun

Code: Select all

$s =~ tr/a-z/A-Z/;

Posted: Fri Sep 14, 2007 11:01 am
by MetroMunkee
Thanks, I'm not using PERL or any programming language though.

Posted: Fri Sep 14, 2007 10:32 pm
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.