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.
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.
Richard S. 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
Write a regular expression to match everything before the first
period.
Substitute it with the uppercased version
str = “helpuser01.hello.txt”
str.sub!(/\A.*?./) { $&.upcase }
puts str
On Sun, Jan 23, 2011 at 11:24 PM, Richard S. [email protected]
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
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 S. [email protected]
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
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
Please use the following regexp instead:
str.sub!( /\A[^.]*/ ) { |s| s.upcase }
str = “hello”
str.sub!( /\A[^.]*/ ) { |s| s.upcase }
puts str
Regards,
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs