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”]
How to do this without loosing the trailing character?
On Dec 6, 10:55pm, Intransition [email protected] 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”]
On Tue, Dec 7, 2010 at 5:55 AM, Intransition [email protected]
wrote:
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 6:02 AM, Intransition [email protected]
wrote:
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 12/06/2010 11:20 PM, Intransition wrote:
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
On Dec 7, 12:07am, Ammar A. [email protected] wrote:
Note also spaces after operators should be ok too. I.e. We want:
[ “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
On Tue, Dec 7, 2010 at 6:47 AM, Ammar A. [email protected] wrote:
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
(\w+)
\s+
([>=<] \s* \d+(?:.\d+)+)
\s+
([>=<] \s* \d+(?:.\d+)+)
}x.match …
Cheers
robert
On Tue, Dec 7, 2010 at 7:20 AM, Intransition [email protected]
wrote:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs