ruby - How do I write a regex that eliminates the space between a number and a colon? -


i want replace space between 1 or 2 numbers , colon followed space, number, or end of line. if have string like,

line = "   0 : 28 : 37.02" 

the result should be:

"   0: 28: 37.02" 

i tried below:

line.gsub!(/(\a|[ \u00a0|\r|\n|\v|\f])(\d?\d)[ \u00a0|\r|\n|\v|\f]:(\d|[ \u00a0|\r|\n|\v|\f]|\z)/, '\2:\3') # => "  0: 28 : 37.02" 

it seems match first ":", second ":" not matched. can't figure out why.

excluding third digit can done negative lookback, since other 1 or 2 digits of variable length, cannot use positive lookback part.

line.gsub(/(?<!\d)(\d{1,2}) (?=:[ \d\$])/, '\1') # => "   0: 28: 37.02" 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -