Hai anh Le wrote:
> I have another example as:
> 
> "The coefficients are then mixed with the data from mixer 51xy, so that 
> the recording equipment 14abc stores the six channel data stream, the 
> scripting data 26aa and 27ac"
> 
> My purpose is to find the position of number have style "123abc", after 
> that I get the string from begin to that position like "The coefficients 
> are then mixed with the data from mixer 51xy,"
> Then I will continued search...
> 
> My code like
> 
> while string.size do
> if string=~ /.../ then
>   temp_string = $` + $&
>   string.gsub!(temp_string, "")
> else
>   break
> end
> 
> 
> So that what I need is $& = "123abc" and not = "123abc and 123abc"

Sorry - that still does not make it clear what you want, especially with 
the portion of the string " ... the scripting data 26aa and 27ac"

Here is some code:

str = <<EOS
The coefficients are then mixed with the data from mixer 51xy, so that
the recording equipment 14abc stores the six channel data stream, the
scripting data 26aa and 27ac
EOS

p str.scan(/.*?\d+[a-z]*/m)

# ["The coefficients are then mixed with the data from mixer 51xy", ", 
so that \nthe recording equipment 14abc", " stores the six channel data 
stream, the \nscripting data 26aa", " and 27ac"]

If you want to keep 26aa but not 27ac, then use reject to strip out the 
match " and 27ac".

Here is another option:

p str.scan(/.*?\d+[a-z]*(?: and \d+[a-z]*)?/m)

# ["The coefficients are then mixed with the data from mixer 51xy", ", 
so that \nthe recording equipment 14abc", " stores the six channel data 
stream, the \nscripting data 26aa and 27ac"]

This time the whole clause up to an including 26aa and 27ac is a 
separate item, and you can strip this out using reject.
-- 
Posted via http://www.ruby-forum.com/.