Morris
Morris

Reputation: 520

Ruby - How to remove space after some characters?

I need to remove white spaces after some characters, not all of them. I want to remove whites spaces after these chars: I,R,P,O. How can I do it?

Upvotes: 0

Views: 61

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110685

" P  $ R   3I&".gsub(/([IRPO])\s+/,'\1')
  #=> " P$ R3I&"

Upvotes: 1

sawa
sawa

Reputation: 168101

"I ".gsub(/(?<=[IRPO]) /, "") # => "I"
"A ".gsub(/(?<=[IRPO]) /, "") # => "A "

Upvotes: 4

Related Questions