Split version requirements

Discussion in 'Ruby' started by Intransition, Dec 7, 2010.

  1. Intransition

    Intransition Guest

    Problem...

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

    How to do this without loosing the trailing character?
    Intransition, Dec 7, 2010
    #1
    1. Advertising

  2. Intransition

    Intransition Guest

    On Dec 6, 10:55=A0pm, Intransition <> wrote:
    > 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"]
    Intransition, Dec 7, 2010
    #2
    1. Advertising

  3. Intransition

    Ammar Ali Guest

    On Tue, Dec 7, 2010 at 5:55 AM, Intransition <> wrote:
    > 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".split(/\s+/)

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

    Regards,
    Ammar
    Ammar Ali, Dec 7, 2010
    #3
  4. Intransition

    Ammar Ali Guest

    On Tue, Dec 7, 2010 at 6:02 AM, Intransition <> wrote:
    >
    >
    > On Dec 6, 10:55=C2=A0pm, Intransition <> wrote:
    >> 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
    Ammar Ali, Dec 7, 2010
    #4
  5. Intransition

    Intransition Guest

    On Dec 7, 12:07=A0am, Ammar Ali <> wrote:
    > On Tue, Dec 7, 2010 at 6:02 AM, Intransition <> wrote:
    >
    > > On Dec 6, 10:55=A0pm, Intransition <> wrote:
    > >> 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 :-(
    Intransition, Dec 7, 2010
    #5
  6. Intransition

    Jeremy Bopp Guest

    On 12/06/2010 11:20 PM, Intransition wrote:
    > On Dec 7, 12:07 am, Ammar Ali <> 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 :-(


    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
    Jeremy Bopp, Dec 7, 2010
    #6
  7. Intransition

    Ammar Ali Guest

    On Tue, Dec 7, 2010 at 7:20 AM, Intransition <> 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
    Ammar Ali, Dec 7, 2010
    #7
  8. On Tue, Dec 7, 2010 at 6:47 AM, Ammar Ali <> wrote:
    > On Tue, Dec 7, 2010 at 7:20 AM, Intransition <> 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.


    +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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Dec 7, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl
    Replies:
    1
    Views:
    361
    Andy Mortimer [MS]
    Feb 7, 2005
  2. Replies:
    2
    Views:
    450
  3. V Green
    Replies:
    0
    Views:
    823
    V Green
    Feb 5, 2008
  4. PA Bear [MS MVP]
    Replies:
    0
    Views:
    929
    PA Bear [MS MVP]
    Feb 5, 2008
  5. MowGreen [MVP]
    Replies:
    5
    Views:
    1,993
    PA Bear [MS MVP]
    Feb 9, 2008
Loading...

Share This Page