Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:36:54+00:00 2026-05-24T08:36:54+00:00

Currently i am splitting a string by pattern, like this: outcome_array=the_text.split(pattern_to_split_by) The problem is

  • 0

Currently i am splitting a string by pattern, like this:

outcome_array=the_text.split(pattern_to_split_by)

The problem is that the pattern itself that i split by, always gets omitted.

How do i get it to include the split pattern itself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:36:55+00:00Added an answer on May 24, 2026 at 8:36 am

    Thanks to Mark Wilkins for inpsiration, but here’s a shorter bit of code for doing it:

    irb(main):015:0> s = "split on the word on okay?"
    => "split on the word on okay?"
    irb(main):016:0> b=[]; s.split(/(on)/).each_slice(2) { |s| b << s.join }; b
    => ["split on", " the word on", " okay?"]
    

    or:

    s.split(/(on)/).each_slice(2).map(&:join)
    

    See below the fold for an explanation.


    Here’s how this works. First, we split on “on”, but wrap it in parentheses to make it into a match group. When there’s a match group in the regular expression passed to split, Ruby will include that group in the output:

    s.split(/(on)/)
    # => ["split", "on", "the word", "on", "okay?"
    

    Now we want to join each instance of “on” with the preceding string. each_slice(2) helps by passing two elements at a time to its block. Let’s just invoke each_slice(2) to see what results. Since each_slice, when invoked without a block, will return an enumerator, we’ll apply to_a to the Enumerator so we can see what the Enumerator will enumerator over:

    s.split(/(on)/).each_slice(2).to_a
    # => [["split", "on"], ["the word", "on"], ["okay?"]]
    

    We’re getting close. Now all we have to do is join the words together. And that gets us to the full solution above. I’ll unwrap it into individual lines to make it easier to follow:

    b = []
    s.split(/(on)/).each_slice(2) do |s|
      b << s.join
    end
    b
    # => ["split on", "the word on" "okay?"]
    

    But there’s a nifty way to eliminate the temporary b and shorten the code considerably:

    s.split(/(on)/).each_slice(2).map do |a|
      a.join
    end
    

    map passes each element of its input array to the block; the result of the block becomes the new element at that position in the output array. In MRI >= 1.8.7, you can shorten it even more, to the equivalent:

    s.split(/(on)/).each_slice(2).map(&:join)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently receiving a string like this: @Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54 And I am splitting it
I have a string like: {A}{B}={C}{D}<{E}{F}<= What I want to do is split that
The requirement is to split strings in Java so that the following this#{s}is#{s}a#{s}string would
I am currently looking into spliting a very long string that could contain HTML
I am working on splitting out an existing, working application that I currently have
I have some strings, entered by users, that may look like this: ++7 7++
I'm splitting a potentially large string (let's say 20MB, though this is entirely arbitrary)
I am currently working on a project that involves splitting a url. I have
I'd like to specify a delimiter for a scanner that splits on some pattern,
The company I work for has several clients. I'm currently splitting my time between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.