[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: removing spaces between single characters with regual expression

Gavin Kistner

9/12/2006 6:03:00 PM

From: ciapecki [mailto:ciapecki@gmail.com]
> 'A B C' -> 'ABC'
> 'AB C' -> 'AB C' (no change)
> 'Teddy Bear A B' -> 'Teddy Bear AB'

irb(main):005:0> s = 'A long cat and a quick mouse met o j simpson on
the t c p stack'
=> "A long cat and a quick mouse met o j simpson on the t c p stack"

irb(main):006:0> s.gsub( /\b([a-z]) (?=[a-z]\b)/i, '\\1' )
=> "A long cat and a quick mouse met oj simpson on the tcp stack"



2 Answers

ciapecki

9/12/2006 7:27:00 PM

0

Thanks Gavin for your solution

> irb(main):005:0> s = 'A long cat and a quick mouse met o j simpson on
> the t c p stack'
> => "A long cat and a quick mouse met o j simpson on the t c p stack"
>
> irb(main):006:0> s.gsub( /\b([a-z]) (?=[a-z]\b)/i, '\\1' )
> => "A long cat and a quick mouse met oj simpson on the tcp stack"

this is exactly what I need.
Is it possible to rewrite it without using lookahead (?=subexp) ?

chris

ciapecki

9/12/2006 10:22:00 PM

0

> irb(main):006:0> s.gsub( /\b([a-z]) (?=[a-z]\b)/i, '\\1' )
> => "A long cat and a quick mouse met oj simpson on the tcp stack"

what about to add some additional characters like '&' to work as
characters?
to make following possible:
"a b & c de" => "ab&c de"

chris