2012/6/7 cyber c. <lists / ruby-forum.com>:
> str = "ABCD12A2012"
>
> I need to divide the string into "ABCD" and "12A2012" i.e. right at the
> start of a number in the string
>
> i can use str.partition(%r(\d)) which gives me ["ABCD","1","2A2012"]. I
> can club the last two sub strings to get the work done. But is there a
> better way to do so?


Regexp.new(/^([a-zA-Z]+)(.+)$/).match(str).captures

=> ["ABCD", "12A2012"]


Or more generic:

match_data = Regexp.new(/^([a-zA-Z]+)(.+)$/).match(str)

match_data[0]  # The whole matched string:
=> "ABCD12A2012"

match_data[1]  # The content within first ( ):
=> "ABCD"

match_data[2]  # The content within second ( ):
=> "12A2012"



Or if you are using Ruby 1.9 (time to move on):

match_data = Regexp.new(/^(?<first_portion>[a-zA-Z]+)(?<second_portion>.+)$/).match(str)

match_data["first_portion"]
=> "ABCD"

match_data["second_portion"]
=> "12A2012"


-- 
Iaki Baz Castillo
<ibc / aliax.net>