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 7994669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:11:40+00:00 2026-06-04T14:11:40+00:00

Given the input: str = foo bar jim jam. jar jee joon. I need

  • 0

Given the input:

str = "foo bar jim jam. jar jee joon."

I need the output of all 2- and 3-word phrases that are separated by spaces:

[ "foo bar", "bar jim", "jim jam", "jar jee", "jee joon",
  "foo bar jim", "bar jim jam", "jar jee joon" ]

Note in particular the lack of “jam jar”, “jim jam jar” and “jam jar jee” in the above, due to the period.

I can’t use str.scan(/\w+/).each_cons(2).map{ |a| a.join(' ') } because that would include "jam jar".

Scanning for /\w+ \w+/ yields ["foo bar", "jim jam", "jar jee"], notably missing “bar jim” and “jee joon”, and highlighting the problem.

The real-world application for this is generating a phrase-based index for a search engine. I want to find all the truly-consecutive words as phrases, excluding those with punctuation separating words.

Edit: Seems like there might be a way to do this in regex/scan via a variation on:

"a b c d".scan(/(?=([abc] [abc]) )[abc]/)
#=> [["a b"], ["b c"]]
  • 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-06-04T14:11:42+00:00Added an answer on June 4, 2026 at 2:11 pm

    I believe this does the job, although it assumes the only punctuation is in the form of periods:

    str.split(".").map do |s|
      pairs_and_triples = []
      s.split.each_cons(2){ |*words| pairs_and_triples << words.join(" ") }
      s.split.each_cons(3){ |*words| pairs_and_triples << words.join(" ")}
      pairs_and_triples
    end.flatten
    

    EDIT or with a little less repitition:

    str.split(".").map do |s|
      [2,3].map do |i|
        s.split.each_cons(i).map{ |*words| words.join(" ") }
      end.flatten
    end.flatten
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to retrieve a regex pattern matched strings from the given input. Lets
Given the following code : .section .rodata str: .string "Hello World!\n" input: .long 2
Given an arbitrary input string I'm meant to find the sum of all numbers
Given an input sequence, what is the best way to find the longest (not
Given the input XML file: <acctInfo> <wfInfo> <aaa>1</aaa> <bbb>1</bbb> <ccc>1</ccc> <ddd>1</ddd> <eee>1</eee> </wfInfo> <acctInfo>
So given this input string: =?ISO-8859-1?Q?TEST=2C_This_Is_A_Test_of_Some_Encoding=AE?= And this function: private string DecodeSubject(string input) {
You are given as input an unsorted array of n distinct numbers, where n
I am given an input, N, i have to find the number of list
Suppose that my Haskell function is given an input, which is supposed to be
I am looking to transform a input xml given below <profile name=default> <color id=forecolor

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.