Split version requirements

Problem...

  "rake >0.8 <1.0".split(/[^>=<]\s+/)
  => ["rak", ">0.", "<1.0"]

How to do this without loosing the trailing character?

Note also spaces after operators should be ok too. I.e. We want:

   "rake > 0.8 < 1.0".split(YOUR_REGEX)
   => ["rake", "> 0.8", "< 1.0"]

···

On Dec 6, 10:55 pm, Intransition <transf...@gmail.com> wrote:

Problem...

"rake >0.8 <1.0".split(/[^>=<]\s+/)
=> ["rak", ">0.", "<1.0"]

How to do this without loosing the trailing character?

Problem...

"rake >0.8 <1.0".split(/[^>=<]\s+/)
=> ["rak", ">0.", "<1.0"]

I can't tell what your goal is exactly, but since the problem is not
the inclusion of the > and < in the result, wouldn't a simple /\s+/
do?

"rake >0.8 <1.0".split(/\s+/)

[ "rake", ">0.8", "<1.0" ]

Regards,
Ammar

···

On Tue, Dec 7, 2010 at 5:55 AM, Intransition <transfire@gmail.com> wrote:

Problem...

"rake >0.8 <1.0".split(/[^>=<]\s+/)
=> ["rak", ">0.", "<1.0"]

How to do this without loosing the trailing character?

Note also spaces after operators should be ok too. I.e. We want:

"rake > 0.8 < 1.0".split(YOUR_REGEX)
=> ["rake", "> 0.8", "< 1.0"]

In that case:

"rake >0.8 <1.0".split(/(?<![>=<])\s+/)

[ "rake", ">0.8", "<1.0" ]

"rake > 0.8 < 1.0".split(/(?<![>=<])\s+/)

[ "rake", "> 0.8", "< 1.0" ]

Regards,
Ammar

···

On Tue, Dec 7, 2010 at 6:02 AM, Intransition <transfire@gmail.com> wrote:

On Dec 6, 10:55 pm, Intransition <transf...@gmail.com> wrote:

Cool, I've never seen '?<!' before. Thanks for the tip.

Of course, just my luck it doesn't work in Ruby 1.8 :frowning:

···

On Dec 7, 12:07 am, Ammar Ali <ammarabu...@gmail.com> wrote:

On Tue, Dec 7, 2010 at 6:02 AM, Intransition <transf...@gmail.com> wrote:

> On Dec 6, 10:55 pm, Intransition <transf...@gmail.com> wrote:
>> Problem...

>> "rake >0.8 <1.0".split(/[^>=<]\s+/)
>> => ["rak", ">0.", "<1.0"]

>> How to do this without loosing the trailing character?

> Note also spaces after operators should be ok too. I.e. We want:

> "rake > 0.8 < 1.0".split(YOUR_REGEX)
> => ["rake", "> 0.8", "< 1.0"]

In that case:

>> "rake >0.8 <1.0".split(/(?<![>=<])\s+/)

[ "rake", ">0.8", "<1.0" ]>> "rake > 0.8 < 1.0".split(/(?<![>=<])\s+/)

[ "rake", "> 0.8", "< 1.0" ]

Convert the negative lookbehind assertion into a positive lookahead
assertion:

irb(main):001:0> "rake >0.8 <1.0".split(/\s+(?=[>=<])/)
=> ["rake", ">0.8", "<1.0"]
irb(main):002:0> "rake > 0.8 < 1.0".split(/\s+(?=[>=<])/)
=> ["rake", "> 0.8", "< 1.0"]

-Jeremy

···

On 12/06/2010 11:20 PM, Intransition wrote:

On Dec 7, 12:07 am, Ammar Ali <ammarabu...@gmail.com> wrote:

In that case:

"rake >0.8 <1.0".split(/(?<![>=<])\s+/)

[ "rake", ">0.8", "<1.0" ]>> "rake > 0.8 < 1.0".split(/(?<![>=<])\s+/)

[ "rake", "> 0.8", "< 1.0" ]

Cool, I've never seen '?<!' before. Thanks for the tip.

Of course, just my luck it doesn't work in Ruby 1.8 :frowning:

Of course, just my luck it doesn't work in Ruby 1.8 :frowning:

I should have mentioned that.

Just a thought, I suspect you might have already considered, why not
just use match instead of split?

/(\w+)\s+([>=<]\s*\d+\.\d+)\s+([>=<]\s*\d+\.\d+)/.match("rake > 0.8 < 1.0").captures

[ "rake", "> 0.8", "< 1.0" ]

Sure it's long and not as elegant as split, but it's clear, I think,
explicit, and cross-version compatible.

Regards,
Ammar

···

On Tue, Dec 7, 2010 at 7:20 AM, Intransition <transfire@gmail.com> wrote:

+1

And if it is too long to read easily we can always use /x:

%r{
  \A
  # leading word:
  (\w+)
  \s+
  # 1st version no:
  ([>=<] \s* \d+(?:\.\d+)+)
  \s+
  # 2nd version no:
  ([>=<] \s* \d+(?:\.\d+)+)
}x.match ...

Cheers

robert

···

On Tue, Dec 7, 2010 at 6:47 AM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Tue, Dec 7, 2010 at 7:20 AM, Intransition <transfire@gmail.com> wrote:

Of course, just my luck it doesn't work in Ruby 1.8 :frowning:

I should have mentioned that.

Just a thought, I suspect you might have already considered, why not
just use match instead of split?

/(\w+)\s+([>=<]\s*\d+\.\d+)\s+([>=<]\s*\d+\.\d+)/.match("rake > 0.8 < 1.0").captures

[ "rake", "> 0.8", "< 1.0" ]

Sure it's long and not as elegant as split, but it's clear, I think,
explicit, and cross-version compatible.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/