Best way to partition a string

Hi,

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?

Hi,

You can use String#split with a lookahead regex and a limit of 2:

str = “ABCD12A2012”
p str.split /(?=\d)/, 2

2012/6/7 cyber c. [email protected]:

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”

I need to divide the string into “ABCD” and “12A2012” i.e. right at the
start of a number in the string

Does this work?

str = “ABCD12A2012”
p str.split(/(\d.*\z)/) #> [“ABCD”, “12A2012”]

Harry

On Fri, Jun 8, 2012 at 1:09 PM, Harry K. [email protected]
wrote:

I need to divide the string into “ABCD” and “12A2012” i.e. right at the
start of a number in the string

I just saw on ruby forum that you already had a solution that you liked.
Your reply did not show up on ruby talk (not in my inbox, anyway.)
Sorry for the noise.

Harry

@jacques1
Thank you very much

“Iñaki Baz C.” [email protected] wrote in post #1063634:

2012/6/7 cyber c. [email protected]:
Regexp.new(/^([a-zA-Z]+)(.+)$/).match(str).captures

Why are you using Regexp.new, when /^([a-zA-Z]+)(.+)$/ already is a
Regexp? This seems nonsensical to me, like String.new(“abc”).

2012/6/8 Jan E. [email protected]:

“Iñaki Baz C.” [email protected] wrote in post #1063634:

2012/6/7 cyber c. [email protected]:
Regexp.new(/^([a-zA-Z]+)(.+)$/).match(str).captures

Why are you using Regexp.new, when /^([a-zA-Z]+)(.+)$/ already is a
Regexp? This seems nonsensical to me, like String.new(“abc”).

Yes, I’m used to store my regexps in some module/class constants and
usually use Regexp.new for some reason I don’t remember right now,
maybe because it allows passing a string which is useful in certain
cases:

Regexp.new(“^([a-zA-Z]+)(.+)$”)

But right, in my previous code I was passing a Regexp instance to
Regexp.new, which is not cool, sure :slight_smile:

On Fri, Jun 8, 2012 at 12:20 PM, Iaki Baz C. [email protected]
wrote:

maybe because it allows passing a string which is useful in certain
cases:

Regexp.new(“^([a-zA-Z]+)(.+)$”)

Like

irb(main):001:0> s = “foo”
=> “foo”
irb(main):002:0> rx = /^([a-zA-Z]+)#{s}(.+)$/
=> /^([a-zA-Z]+)foo(.+)$/
irb(main):003:0> puts rx
(?-mix:^([a-zA-Z]+)foo(.+)$)
=> nil

?

Kind regards

robert