[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

downcase part of a string

ilhamik

10/22/2006 11:20:00 AM

hi,
I want to downcase a string but without specific parts.
for example:
msg = "THIS is a Text and (NO Change HERE) HELP"

after downcase it should look like "this is a text and (NO Change HERE)
help"

I don't want to downcase the letters in parentheses.
How can i do that, i tried it with regular expressions but can't do
it.

Thanks for any help

36 Answers

Kalman Noel

10/22/2006 11:53:00 AM

0

ilhamik:
> hi,
> I want to downcase a string but without specific parts.
> for example:
> msg = "THIS is a Text and (NO Change HERE) HELP"

If the parentheses occur only once:

if msg =~ /\(.*?\)/
$~.pre_match.downcase + $~[0] + $~.post_match.downcase
end

Kalman

Peter Szinek

10/22/2006 12:14:00 PM

0

ilhamik wrote:
> hi,
> I want to downcase a string but without specific parts.
> for example:
> msg = "THIS is a Text and (NO Change HERE) HELP"

Hi,

This is kind of old school and I am sure there are nicer rubyish
solutions for it, but at least it works for multiple parentheses as well:

original = msg.scan(/\(.+?\)/)
msg.downcase!
altered = msg.scan(/\(.+?\)/)
original.each_with_index { |stuff, i| msg.sub!(altered[i],stuff) }

--
Peter
http://www.rubyra...

ilhamik

10/22/2006 12:14:00 PM

0


No, they can occur more then onece.

Kalman Noel wrote:
> ilhamik:
> > hi,
> > I want to downcase a string but without specific parts.
> > for example:
> > msg = "THIS is a Text and (NO Change HERE) HELP"
>
> If the parentheses occur only once:
>
> if msg =~ /\(.*?\)/
> $~.pre_match.downcase + $~[0] + $~.post_match.downcase
> end
>
> Kalman

ilhamik

10/22/2006 12:29:00 PM

0

Thanks Peter, it works fine.


Peter Szinek wrote:
> ilhamik wrote:
> > hi,
> > I want to downcase a string but without specific parts.
> > for example:
> > msg = "THIS is a Text and (NO Change HERE) HELP"
>
> Hi,
>
> This is kind of old school and I am sure there are nicer rubyish
> solutions for it, but at least it works for multiple parentheses as well:
>
> original = msg.scan(/\(.+?\)/)
> msg.downcase!
> altered = msg.scan(/\(.+?\)/)
> original.each_with_index { |stuff, i| msg.sub!(altered[i],stuff) }
>
> --
> Peter
> http://www.rubyra...

James Gray

10/22/2006 1:43:00 PM

0

On Oct 22, 2006, at 7:30 AM, ilhamik wrote:

> Thanks Peter, it works fine.

You missed Tim Bray's RubyConf talk. According to him we should,
never be using the case changing methods. "Just don't do it!" ;)

James Edward Gray II

Scott

10/22/2006 6:00:00 PM

0

Certainly not pretty with that funky regex, but it works:

msg = "THIS is a Text and (NO Change HERE) HELP (Not here Either)"

msg.gsub!(/([^\(]*(?!\())|(\(.*?\))|(\)[^\)]*\))/) do |m|
m[0] == 40 ? m : m.downcase
end

- Scott

ilhamik wrote:
> hi,
> I want to downcase a string but without specific parts.
> for example:
> msg = "THIS is a Text and (NO Change HERE) HELP"
>
> after downcase it should look like "this is a text and (NO Change HERE)
> help"
>
> I don't want to downcase the letters in parentheses.
> How can i do that, i tried it with regular expressions but can't do
> it.
>
> Thanks for any help

Mike Durham

10/22/2006 9:47:00 PM

0

James Edward Gray II wrote:
> On Oct 22, 2006, at 7:30 AM, ilhamik wrote:
>
>> Thanks Peter, it works fine.
>
> You missed Tim Bray's RubyConf talk. According to him we should, never
> be using the case changing methods. "Just don't do it!" ;)
>
> James Edward Gray II
>
Why not? What reason did he give?
Cheers

Wilson Bilkovich

10/23/2006 1:17:00 AM

0

On 10/22/06, Mike Durham <mdurham@people.net.au> wrote:
> James Edward Gray II wrote:
> > On Oct 22, 2006, at 7:30 AM, ilhamik wrote:
> >
> >> Thanks Peter, it works fine.
> >
> > You missed Tim Bray's RubyConf talk. According to him we should, never
> > be using the case changing methods. "Just don't do it!" ;)
> >
> > James Edward Gray II
> >
> Why not? What reason did he give?

The problem is that proper upcasing and downcasing of characters is
locale-dependent, not just encoding or language-dependent.

As examples, he mentioned that the uppercase version of accented
characters varies from area to area in France. Also, in Turkish,
there are four different cases of 'i', not just two.. and which is
correct depends on the jurisdiction.
Determining the locale in a correct way is really, really hard. Tim
Bray says it's basically impossible. Also, all of these rules make
any decent upcase/downcase function ruinously slow.

He shared a story about the original version of XML. At the time, it
was case-insensitive. The very first XML library was running horribly
slow. After profiling, they found that it was spending 90% of its
time in the Java downcase routine. After that, XML was made
case-sensitive.

Mike Durham

10/23/2006 2:21:00 AM

0

Wilson Bilkovich wrote:
> On 10/22/06, Mike Durham <mdurham@people.net.au> wrote:
>> James Edward Gray II wrote:
>> > On Oct 22, 2006, at 7:30 AM, ilhamik wrote:
>> >
>> >> Thanks Peter, it works fine.
>> >
>> > You missed Tim Bray's RubyConf talk. According to him we should, never
>> > be using the case changing methods. "Just don't do it!" ;)
>> >
>> > James Edward Gray II
>> >
>> Why not? What reason did he give?
>
> The problem is that proper upcasing and downcasing of characters is
> locale-dependent, not just encoding or language-dependent.
>
> As examples, he mentioned that the uppercase version of accented
> characters varies from area to area in France. Also, in Turkish,
> there are four different cases of 'i', not just two.. and which is
> correct depends on the jurisdiction.
> Determining the locale in a correct way is really, really hard. Tim
> Bray says it's basically impossible. Also, all of these rules make
> any decent upcase/downcase function ruinously slow.
>
> He shared a story about the original version of XML. At the time, it
> was case-insensitive. The very first XML library was running horribly
> slow. After profiling, they found that it was spending 90% of its
> time in the Java downcase routine. After that, XML was made
> case-sensitive.
>
Thanks Wilson, that explains everything. I'd never thought about
problems like that.
Cheers, Mike

James Gray

10/24/2006 5:20:00 PM

0

On Oct 22, 2006, at 8:16 PM, Wilson Bilkovich wrote:

> On 10/22/06, Mike Durham <mdurham@people.net.au> wrote:
>> James Edward Gray II wrote:
>> > On Oct 22, 2006, at 7:30 AM, ilhamik wrote:
>> >
>> >> Thanks Peter, it works fine.
>> >
>> > You missed Tim Bray's RubyConf talk. According to him we
>> should, never
>> > be using the case changing methods. "Just don't do it!" ;)
>> >
>> > James Edward Gray II
>> >
>> Why not? What reason did he give?
>
> The problem is that proper upcasing and downcasing of characters is
> locale-dependent, not just encoding or language-dependent.

Yes, this is basically it.

Tim Bray feels that case changing is more or less impossible in the
practical sense. When you get around to downcasing that string a
user entered into your web form a month back, are you going to know
if that string was encoded in a Turkish local (critical info if it
contains an "i")?

Even if it were possible, Tim suggests that it's a performance
killer. See Java, which tries to address as many rules as it
possibly can, for proof.

James Edward Gray II