From: Joel VanderWerf Date: 2003-09-28T04:27:16+09:00 Subject: Re: regexp needed to split letters from numbers Rodrigo B. de Oliveira wrote: >>irb(main):001:0> s = "clonidine300 mg" >>=> "clonidine300 mg" >>irb(main):002:0> s[/(?=\d+ mg)/] = " " >>=> " " >> > > > What's exactly happening here? String#[]=, as in "s1[pat] = s2", is a destructive slice operator. It replaces the first match of pat with the r.h.s. (raising IndexError if no match). In this case, pat is /(?=\d+ mg)/, which has a lookahead pattern (?=...). This lookahead expression matches the point in the string *just before* the match of /\d+ mg/, but it doesn't consume the "300 mg". So slicing out the match (which is empty) and substituting " " has the effect of inserting a space before the match of /\d+ mg/.