[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

RegEx Question

Justin To

6/25/2008 11:07:00 PM

Hi, my practice program is suppose to read in versions like:

4.0
v4. 5
v 4.23

^each = p

t="v"
p.gsub(/[^(v)]/i) {|m| t.concat(m)}

how do I, in the regEx, ignore white spaces?

I want the output to look like this:

v4.0
v4.5
v4.23

Thanks!
--
Posted via http://www.ruby-....

3 Answers

Justin To

6/25/2008 11:21:00 PM

0


p.gsub(/[^(v| )]/i) {|m| t.concat(m)}


Oh, I got it... but now I'm stuck on how to remove '.' if it's the last
piece of the string

e.g. "v4.5."

I want to remove the last "."

Thanks
--
Posted via http://www.ruby-....

Rodrigo Bermejo

6/26/2008 12:36:00 AM

0

Justin To wrote:
>
> p.gsub(/[^(v| )]/i) {|m| t.concat(m)}
>
>
> Oh, I got it... but now I'm stuck on how to remove '.' if it's the last
> piece of the string
>
> e.g. "v4.5."
>
> I want to remove the last "."
>
> Thanks

"v4.5.".gsub(/\.$/,"")
--
Posted via http://www.ruby-....

David A. Black

6/26/2008 12:46:00 AM

0

Hi --

On Thu, 26 Jun 2008, Justin To wrote:

>
> p.gsub(/[^(v| )]/i) {|m| t.concat(m)}
>
>
> Oh, I got it... but now I'm stuck on how to remove '.' if it's the last
> piece of the string
>
> e.g. "v4.5."
>
> I want to remove the last "."

If you've just got that string and want to remove the last '.', as a
separate operation, you can do:

str.chomp!('.')


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.r... for details and updates!