Split version requirements

I

Intransition

Problem...

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

How to do this without loosing the trailing character?
 
I

Intransition

Problem...

=A0 "rake >0.8 <1.0".split(/[^>=3D<]\s+/)
=A0 =3D> ["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)
=3D> ["rake", "> 0.8", "< 1.0"]
 
A

Ammar Ali

Problem...

=C2=A0"rake >0.8 <1.0".split(/[^>=3D<]\s+/)
=C2=A0=3D> ["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" ]

Regards,
Ammar
 
A

Ammar Ali

Problem...

=C2=A0 "rake >0.8 <1.0".split(/[^>=3D<]\s+/)
=C2=A0 =3D> ["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:

=C2=A0 "rake > 0.8 < 1.0".split(YOUR_REGEX)
=C2=A0 =3D> ["rake", "> 0.8", "< 1.0"]

In that case:
"rake >0.8 <1.0".split(/(?<![>=3D<])\s+/) [ "rake", ">0.8", "<1.0" ]
"rake > 0.8 < 1.0".split(/(?<![>=3D<])\s+/)
[ "rake", "> 0.8", "< 1.0" ]

Regards,
Ammar
 
I

Intransition

Problem...
=A0 "rake >0.8 <1.0".split(/[^>=3D<]\s+/)
=A0 =3D> ["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:
=A0 "rake > 0.8 < 1.0".split(YOUR_REGEX)
=A0 =3D> ["rake", "> 0.8", "< 1.0"]

In that case:
"rake >0.8 <1.0".split(/(?<![>=3D<])\s+/)

[ "rake", ">0.8", "<1.0" ]>> "rake > 0.8 < 1.0".split(/(?<![>=3D<])\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 :-(
 
J

Jeremy Bopp

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 :-(

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
 
A

Ammar Ali

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

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
 
R

Robert Klemme

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

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.

+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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top