Change to upper case
Moderator: General Moderators
-
MetroMunkee
- Forum Newbie
- Posts: 3
- Joined: Thu Sep 13, 2007 5:05 pm
Change to upper case
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!!
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!!
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
preg_replace_callback() would be the best course of action.
you can't use strtoupper ? wasn't sure if you were using php in any of this or not..
Code: Select all
$string=strtr($string,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ");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>You could use the phputf8 strtopupper function which takes accented characters into account.
From the top of my head, try this:
From the top of my head, try this:
Code: Select all
preg_replace('/xxx([a-z]+)yyy/e', 'strtoupper(\'$1\')', $str);- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
As I've said before, avoid the e pattern modifier in preference to using preg_replace_callback().
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Okay, thanks for the tip. Apparently it's much faster indeed.feyd wrote:As I've said before, avoid the e pattern modifier in preference to using preg_replace_callback().
http://php.net/manual/en/function.preg- ... .php#65182
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.stereofrog wrote:How "accented" characters are handled depends on your locale.mrkite wrote: I wish strtoupper were smarter. It won't handle any accented characters.
-
MetroMunkee
- Forum Newbie
- Posts: 3
- Joined: Thu Sep 13, 2007 5:05 pm
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?
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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
My insistence is more about security issue avoidance than speed related, actually.GeertDD wrote:Okay, thanks for the tip. Apparently it's much faster indeed.
http://php.net/manual/en/function.preg- ... .php#65182