kid_drew
kid_drew

Reputation: 3995

Regular expression to remove spaces between boundaries

I have a string of this form:

101 E 11th St #201 B, Austin, TX 78702

I want to remove any white space between the pound sign and the first comma, so the string will become this:

101 E 11th St #201B, Austin, TX 78701

I know how to do a string replace in Ruby, but I don't know the regex to match only spaces between those two characters.

Upvotes: 0

Views: 146

Answers (1)

hwnd
hwnd

Reputation: 70722

You could use a callback to do this ...

s = '101 E 11th St #2 01   B, Austin, TX 78702'
s.sub(/#[^,]+/) {|m| m.gsub(/\s/, '')}
# => "101 E 11th St #201B, Austin, TX 78702"

Upvotes: 2

Related Questions