How to convert letters to uppercase?

Im unfamiliar with regular expressions.

How would I be able to change all letters to uppercase to the first
period?

Before:

helpuser01.hello.txt

After:

HELPUSER01.hello.txt

Any advice would be dearly appreciated.

···

--
Posted via http://www.ruby-forum.com/.

Richard Sandoval wrote in post #976947:

Im unfamiliar with regular expressions.

How would I be able to change all letters to uppercase to the first
period?

Before:

helpuser01.hello.txt

After:

HELPUSER01.hello.txt

1. Write a regular expression to match everything before the first
period.

2. Substitute it with the uppercased version

str = "helpuser01.hello.txt"
str.sub!(/\A.*?\./) { $&.upcase }
puts str

···

--
Posted via http://www.ruby-forum.com/\.

ruby-1.9.2-p0 > a = "helpuser01.hello.txt"
=> "helpuser01.hello.txt"
ruby-1.9.2-p0 > a.sub(/.*?\./) {|m| m.upcase}
=> "HELPUSER01.hello.txt"

martin

···

On Sun, Jan 23, 2011 at 11:24 PM, Richard Sandoval <skolopen@yahoo.com> wrote:

Im unfamiliar with regular expressions.

How would I be able to change all letters to uppercase to the first
period?

Before:

helpuser01.hello.txt

After:

HELPUSER01.hello.txt

Perhaps much less efficient, but 'easier' for newbie :
a="helpuser01.hello.txt"
a=a.split(".",2)
a[0]=a[0].upcase
a=a.compact.join(".")

Martin DeMello wrote in post #976951:

···

On Sun, Jan 23, 2011 at 11:24 PM, Richard Sandoval <skolopen@yahoo.com> > wrote:

After:

HELPUSER01.hello.txt

ruby-1.9.2-p0 > a = "helpuser01.hello.txt"
=> "helpuser01.hello.txt"
ruby-1.9.2-p0 > a.sub(/.*?\./) {|m| m.upcase}
=> "HELPUSER01.hello.txt"

martin

--
Posted via http://www.ruby-forum.com/\.

Hi, Richard

When the string doesn't contain a period, you want to change all
letters to uppercase? or not?
If you want to do, the following code is not good for you:

str.sub!(/\A.*?\./) { $&.upcase }

str = "hello"
str.sub!(/\A.*?\./) { $&.upcase }
puts str
  # hello

Please use the following regexp instead:

str.sub!( /\A[^\.]*/ ) { |s| s.upcase }

str = "hello"
str.sub!( /\A[^\.]*/ ) { |s| s.upcase }
puts str
  # HELLO

Regards,

···

--
信岡 ゆう (NOBUOKA Yu)