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

  • Home
  • SEARCH
  • 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 6058655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:37:23+00:00 2026-05-23T08:37:23+00:00

I have a string in Rails, e.g. This is a Twitter message. #books War

  • 0

I have a string in Rails, e.g. “This is a Twitter message. #books War & Peace by Leo Tolstoy. I love this book!”, and I want to parse the text and extract only certain phrases, like “War & Peace by Leo Tolstoy”.

Is this a matter of using Regex and lifting the text between “#books” to “.”?

What if there’s no structure to the message, like:
“This is a Twitter message #books War & Peace by Leo Tolstoy I love this book!” or
“This is a Twitter message. I love the book War & Peace by Leo Tolstoy #books”
How can I reliably pull the phrase “War & Peace by Leo Tolstoy” without knowing the phrase ex ante.

Are there any gems, methods, etc. that can help me do this?

At the very least, what would you call what I’m trying to do? It will help me search for a solution on Google. I’ve tried a few searches on “parsing” with no luck.

— edit —
based on @rogeliog suggestion, I will add the following:

I can live with the garbage text that comes after #books, but nothing before. I tried “match.(/#books.*/)” — results here: http://www.rubular.com/r/gM7oSZxF5M.

But how can I capture Result #6? (e.g., when someone puts #books at the end of the sentence)?

Is there a way for me to do an if-then with regex? Something like:

if [#books is at the end of the message],

then [take the last 10 words preceding #books],

else [match.(/#books.*/)]

If you offer a regex, please post your solution via a permalink using rubular.com

  • 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-23T08:37:23+00:00Added an answer on May 23, 2026 at 8:37 am

    I Think that you are trying to parse some pretty complex variations. Do you have a DB with all the book titles? That will help allot.

    To get out the title from the first example(“This is a Twitter message. #books War & Peace by Leo Tolstoy. I love this book!”) you can simply:

    "This is a Twitter message. #books War & Peace by Leo Tolstoy. I love this book".match(/#book.*\./).to_s.gsub("#books",'')
    

    That will return: ” War & Peace by Leo Tolstoy.”

    If you want to do an if else statement depending if #books is at the end or not, you can:

    if text.match(/#books$/)
      puts text.match(/([^\s]*\s){10}(#books$)/).to_s
    else
      puts text.match(/#books.*/).to_s.gsub("#books",'')
    end
    

    That will give you the last 10 words preceding books if #books is at the end, and whatever it is after #books if it is not at the end

    I dont really have a better idea, hope that works for you, let me know:)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
Hi I have a JSON string that looks like this (Usingt Rails and a
I have a string string = '1234432198766789' I want it to look like this:
I have a rails application which is still showing the cachebusting numeric string at
I have string that I want to chop to array of substrings of given
Imagine I have String in C#: I Don’t see ya.. I want to remove
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I have a string, say '123' , and I want to convert it to
I have a fairly complex Rails app built on top of Twitter's API, and
I have recently tried sharpening my rails skills with this tool: http://github.com/edgecase/ruby_koans but I

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.